home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Think Class Libraries / WASTE TCL 1.8 / WASTE TCL 1.8 ƒ / CWASTEText.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-12  |  74.9 KB  |  3,102 lines  |  [TEXT/MMCC]

  1. /********************************************************\
  2.  CWASTEText.cpp
  3.  
  4.     by Dan Crevier
  5.     version 1.8
  6.     11/4/95
  7.  
  8.     Roms    95/11/08    adapted to THINK C / TCL 1.1.3
  9. \********************************************************/
  10.  
  11. #ifdef TCL_PCH
  12. #include <TCLHeaders>
  13. #endif
  14.  
  15. #include "CWASTEText.h"
  16. #if WASTE_VERSION >= 0x01100000
  17. #include "CWASTETask.h"
  18. #else
  19. #include "CWASTEEditTask.h"
  20. #include "CWASTEStyleTask.h"
  21. #endif
  22.  
  23. #ifndef __ERRORS__
  24. #include <Errors.h>
  25. #endif
  26. #ifndef __SCRAP__
  27. #include <Scrap.h>
  28. #endif
  29. #ifndef __FONTS__
  30. #include <Fonts.h>
  31. #endif
  32. #ifndef __DRAG__
  33. #include <Drag.h>
  34. #endif
  35. #ifndef __SOUND__
  36. #include <Sound.h>
  37. #endif
  38. #ifndef __ICONS__
  39. #include <Icons.h>
  40. #endif
  41. #ifndef __SCRIPT__
  42. #include <Script.h>
  43. #endif
  44. #ifndef __TEXTSERVICES__
  45. #include <TextServices.h>
  46. #endif
  47.  
  48. #include "CClipboard.h"
  49. #include "CBartender.h"
  50. #include "Commands.h"
  51. #include "TCLUtilities.h"
  52. #include "Constants.h"
  53. #include "CScrollPane.h"
  54. #include "Global.h"
  55. #include "CPrinter.h"
  56. #include "CWindow.h"
  57.  
  58. // Global Variables
  59. extern CClipboard    *gClipboard;    // Copies and Pastes data
  60. extern EventRecord  gLastMouseUp;
  61. extern CBureaucrat    *gGopher;
  62. extern CBartender    *gBartender;
  63.  
  64. extern long        gSleepTime;
  65. extern short        gUsingTSM;
  66. extern Boolean    gHasDragAndDrop;
  67.  
  68. #if WASTE_OBJECT_ARCHIVE
  69. #include "WASTE_Objects.h"
  70. #endif
  71.  
  72. #if WASTE_VERSION == 0x01100000
  73.     // undocumented WASTE routine
  74.     // Note: You need to #define CWASTE if you are using CWASTE 1.0
  75.     #ifdef CWASTE
  76.         extern "C"
  77.         {
  78.             long _WEOffsetToLine(long offset, WEHandle hWE);
  79.         }
  80.     #else
  81.         pascal long _WEOffsetToLine(long offset, WEHandle hWE);
  82.     #endif
  83. #endif
  84.  
  85. #if WASTE_TABS
  86. #include "WETabs.h"
  87. #endif
  88.  
  89. #if WASTE_VERSION >= 0x01100000
  90. // for drag and drop
  91. static DragTrackingHandler trackingUPP = NULL;
  92. static DragReceiveHandler receiveUPP = NULL;
  93. static pascal OSErr WASTETrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
  94.     void *handlerRefCon, DragReference theDrag);
  95. static pascal OSErr WASTEReceiveHandler(WindowPtr theWindow, void *handlerRefCon, DragReference theDrag);
  96.  
  97. #define kTypePicture        'PICT'
  98. #define kTypeSound            'snd '
  99.  
  100. #if WASTE_OBJECTS && !WASTE_OBJECT_ARCHIVE
  101.     // handlers for pictures and sounds
  102.     #define kSoundIconNumber    777
  103.     static pascal OSErr HandleNewPicture(Point *defaultObjectSize, WEObjectReference objectRef);
  104.     static pascal OSErr HandleDisposePicture(WEObjectReference objectRef);
  105.     static pascal OSErr HandleDrawPicture(Rect *destRect, WEObjectReference objectRef);
  106.     static pascal OSErr HandleNewSound(Point *defaultObjectSize, WEObjectReference objectRef);
  107.     static pascal OSErr HandleDisposeSound(WEObjectReference objectRef);
  108.     static pascal OSErr HandleClickSound(Point hitPt, short modifiers, long clickTime,
  109.         WEObjectReference objectRef);
  110.     static pascal OSErr HandleDrawSound(Rect *destRect, WEObjectReference objectRef);
  111. #endif // WASTE_OBJECTS && !WASTE_OBJECT_ARCHIVE
  112.  
  113. #endif // WASTE 1.1
  114.  
  115. CWASTEText *CWASTEText::curWASTEText = NULL;
  116.  
  117. #if TCL_VERSION >= 0x02000000
  118. TCL_DEFINE_CLASS_D1(CWASTEText, CAbstractText);
  119. #endif
  120.  
  121. RGBColor RGBBlack={0,0,0};
  122. RGBColor RGBRed={65535,0,0};
  123. RGBColor RGBGreen={0,65535,0};
  124. RGBColor RGBBlue={0,0,65535};
  125. RGBColor RGBCyan={0,65535,65535};
  126. RGBColor RGBMagenta={65535,0,65535};
  127. RGBColor RGBYellow={65535,65535,0};
  128.  
  129. /********************************************************\
  130.  CWASTEText() - default constructor
  131.      You must call IWASTEText if you use this
  132. \********************************************************/
  133.  
  134. #ifndef THINK_C
  135. CWASTEText::CWASTEText()
  136. {
  137.     macWE = NULL;
  138.     spacingCmd = cmdSingleSpace;
  139.     alignCmd = cmdAlignLeft;
  140.  
  141.     wholeLines = false;
  142.     
  143.     fixedLineHeights = false;
  144.     
  145.     cFirstTaskIndex = 1;  // so we get undo strings
  146.     
  147.     TCL_END_CONSTRUCTOR
  148. }
  149.  
  150. /********************************************************\
  151.  CWASTEText - constructor
  152.      You cannot call IWASTEText if you use this
  153. \********************************************************/
  154.  
  155. CWASTEText::CWASTEText(
  156.     CView            *anEnclosure,
  157.     CBureaucrat        *aSupervisor,
  158.     short            aWidth,
  159.     short            aHeight,
  160.     short            aHEncl,
  161.     short            aVEncl,
  162.     SizingOption    aHSizing,
  163.     SizingOption    aVSizing,
  164.     short            aLineWidth,
  165.     Boolean            aScrollHoriz,
  166.     TextStyle         *tStyle,
  167.     Boolean            supportObjects,
  168.     Boolean            supportUndo,
  169.     Boolean            supportDragAndDrop,
  170.     Boolean            outlineHighliting,
  171.     Boolean            drawOffscreen)
  172.  
  173.     :    CAbstractText(anEnclosure, aSupervisor,
  174.                      aWidth, aHeight, aHEncl, aVEncl, aHSizing, aVSizing,
  175.                      aLineWidth, aScrollHoriz)
  176. {
  177.     macWE = NULL;
  178.     spacingCmd = cmdSingleSpace;
  179.     alignCmd = cmdAlignLeft;
  180.  
  181.     wholeLines = false;
  182.     fixedLineHeights = false;
  183.  
  184.     SetPort(macPort);
  185.  
  186.     if (tStyle)
  187.     {
  188.         TextFont(tStyle->tsFont);
  189.         TextSize(tStyle->tsSize);
  190.         TextFace(tStyle->tsFace);
  191.         if (gSystem.hasColorQD)
  192.             RGBForeColor(&tStyle->tsColor);
  193.     }
  194.  
  195.     
  196.     IWASTETextX(supportObjects, supportUndo, supportDragAndDrop, outlineHighliting,
  197.         drawOffscreen);
  198.  
  199.     cFirstTaskIndex = 1;  // so we get undo strings
  200.     
  201.     TCL_END_CONSTRUCTOR
  202. }
  203. #endif // THINK_C
  204.  
  205. /********************************************************\
  206.  ~CWASTEText - destructor
  207. \********************************************************/
  208.  
  209. #ifndef THINK_C
  210. CWASTEText::~CWASTEText()
  211. #else
  212. void CWASTEText::Dispose()
  213. #endif // THINK_C
  214. {
  215.  
  216. #if TCL_VERSION >= 0x02000000
  217.     TCL_START_DESTRUCTOR
  218. #endif
  219.  
  220.     if (macWE)
  221.     {
  222.         WEDispose(macWE);
  223.         macWE = NULL;
  224. #if WASTE_VERSION >= 0x01100000
  225.         RemoveDragHandlers();
  226. #endif
  227.     }
  228.     
  229. #ifdef THINK_C
  230.     CAbstractText::Dispose();
  231. #endif
  232.  
  233. }
  234.  
  235. /********************************************************\
  236.  IWASTEText - initialize CWASTEText
  237.      Use this only if you use the default constructor
  238. \********************************************************/
  239.  
  240. void CWASTEText::IWASTEText(
  241.     CView            *anEnclosure,
  242.     CBureaucrat        *aSupervisor,
  243.     short            aWidth,
  244.     short            aHeight,
  245.     short            aHEncl,
  246.     short            aVEncl,
  247.     SizingOption    aHSizing,
  248.     SizingOption    aVSizing,
  249.     short            aLineWidth,
  250.     TextStyle         *tStyle,
  251.     Boolean            supportObjects,
  252.     Boolean            supportUndo,
  253.     Boolean            supportDragAndDrop,
  254.     Boolean            outlineHighliting,
  255.     Boolean            drawOffscreen)
  256. {
  257.  
  258. #ifdef THINK_C
  259.     macWE = NULL;
  260.     spacingCmd = cmdSingleSpace;
  261.     alignCmd = cmdAlignLeft;
  262.  
  263.     wholeLines = false;
  264.     
  265.     fixedLineHeights = false;
  266.     
  267.     cFirstTaskIndex = 1;  // so we get undo strings
  268. #endif // THINK_C
  269.  
  270.     CAbstractText::IAbstractText(anEnclosure, aSupervisor,
  271.                      aWidth, aHeight, aHEncl, aVEncl, aHSizing, aVSizing,
  272.                      aLineWidth);
  273.     if (tStyle)
  274.     {
  275.         TextFont(tStyle->tsFont);
  276.         TextSize(tStyle->tsSize);
  277.         TextFace(tStyle->tsFace);
  278.         if (gSystem.hasColorQD)
  279.             RGBForeColor(&tStyle->tsColor);
  280.     }
  281.  
  282.     IWASTETextX(supportObjects, supportUndo, supportDragAndDrop, outlineHighliting, drawOffscreen);
  283. }
  284.  
  285. /********************************************************\
  286.  IViewTemp - initialize from View resource
  287. \********************************************************/
  288.  
  289. void CWASTEText::IViewTemp(CView *anEnclosure, CBureaucrat *aSupervisor,
  290.                         Ptr viewData)
  291. {
  292.     CAbstractText::IViewTemp(anEnclosure, aSupervisor, viewData);
  293.  
  294.     macWE = NULL;
  295.     spacingCmd = cmdSingleSpace;
  296.     alignCmd = cmdAlignLeft;
  297.     fixedLineHeights = FALSE;
  298.  
  299.     IWASTETextX(true, true, true, true, false); // maybe these features could be added to 
  300.                                                 // a new resource type...
  301. }
  302.  
  303. /********************************************************\
  304.  IWASTETextX - initialization common to above routines
  305. \********************************************************/
  306.  
  307. void CWASTEText::IWASTETextX(Boolean supportObjects, Boolean supportUndo,
  308.     Boolean supportDragAndDrop, Boolean outlineHighliting, Boolean drawOffscreen)
  309. {
  310.     LongRect            tLongFrame;
  311.     Boolean                saveAllocState;
  312.     OSErr                err;
  313.     long                flags;
  314.     void                *thisP; // pointer to this
  315. #if WASTE_VERSION >= 0x01100000
  316.     static WEClickLoopUPP        clickLoop = NULL;
  317.     static WETSMPreUpdateUPP    preProc = NULL;
  318.     static WETSMPostUpdateUPP    postProc = NULL;
  319. #if !WASTE_OBJECT_ARCHIVE
  320.     static WENewObjectUPP        newPICTUPP = NULL;
  321.     static WEDisposeObjectUPP    disposePICTUPP = NULL;
  322.     static WEDrawObjectUPP        drawPICTUPP = NULL;
  323.     static WENewObjectUPP        newSndUPP = NULL;
  324.     static WEDisposeObjectUPP    disposeSndUPP = NULL;
  325.     static WEDrawObjectUPP        drawSndUPP = NULL;
  326.     static WEClickObjectUPP        clickSndUPP = NULL;
  327. #endif
  328. #else
  329.     WEClickLoopProcPtr    clickLoop;
  330.     WETSMPreUpdateProcPtr preProc;
  331.     WETSMPostUpdateProcPtr postProc;
  332. #endif
  333.  
  334. #if TCL_VERSION < 0x02000000
  335.     WEReference    theWERef;
  336. #endif
  337.  
  338. #if WASTE_VERSION >= 0x01100000
  339.     dragHandlersInstalled = false;
  340.     hasUndoSupport = supportUndo;
  341. #endif
  342.  
  343.     textMargin = 0;
  344.  
  345.     ForceNextPrepare();
  346.  
  347.     UseLongCoordinates(TRUE);
  348.  
  349.     // Figure out size of of rectangle
  350.     GetAperture(&tLongFrame);
  351.  
  352.     // create WEHandle
  353.     saveAllocState = SetAllocation(kAllocCanFail);
  354.     
  355.     flags = (supportUndo ? weDoUndo : 0) |
  356.             (supportDragAndDrop ? weDoDragAndDrop : 0) |
  357.             (outlineHighliting ? weDoOutlineHilite : 0) |
  358.             (drawOffscreen ? weDoDrawOffscreen : 0); 
  359.             
  360. #if TCL_VERSION < 0x02000000
  361.     err = WENew(&tLongFrame, &tLongFrame, flags, &theWERef);
  362.     macWE = theWERef;
  363. #else
  364.     err = WENew(&tLongFrame, &tLongFrame, flags, &macWE);
  365. #endif
  366.  
  367.     SetAllocation(saveAllocState);
  368.     FailOSErr(err);
  369.  
  370.     // set the refcon to this
  371.     thisP = (void *)this;
  372.     WESetInfo(weRefCon, &thisP, macWE);
  373.  
  374.     SetWholeLines(wholeLines);
  375.  
  376.     AdjustBounds();
  377.  
  378.     autoRefresh = lineWidth <= 0;
  379.  
  380.     // •• Install handlers ••
  381. #if WASTE_VERSION >= 0x01100000
  382.     // if first time, make UPP's
  383.     if (clickLoop == NULL)
  384.     {
  385.         clickLoop = NewWEClickLoopProc(WEClickLoop);
  386.         preProc = NewWETSMPreUpdateProc(WEPreUpdate);
  387.         postProc = NewWETSMPostUpdateProc(WEPostUpdate);
  388.     }
  389. #else
  390.     preProc = &WEPreUpdate;
  391.     postProc = &WEPostUpdate;
  392.     clickLoop = &WEClickLoop;
  393. #endif
  394.  
  395.     WESetInfo(weTSMPreUpdate, (Ptr)&preProc, macWE);
  396.     WESetInfo(weTSMPostUpdate, (Ptr)&postProc, macWE);
  397.     WESetInfo(weClickLoop, (Ptr)&clickLoop, macWE);
  398.  
  399.     CalcWERects();
  400.     if (lineWidth < 0)
  401.     {
  402.         saveAllocState = SetAllocation(kAllocCanFail);
  403.         err = WECalText(macWE);
  404.         SetAllocation(saveAllocState);
  405.     }
  406.  
  407. #if WASTE_TABS
  408. #if WASTE_AUTO_TABS
  409.     InstallTabHandlers();
  410. #endif // WASTE_AUTO_TABS
  411. #endif // WASTE_TABS
  412.  
  413. #if WASTE_VERSION >= 0x01100000
  414.  
  415.     if (supportObjects)
  416.     {
  417. #if WASTE_OBJECT_ARCHIVE
  418.         FailOSErr(InstallAllWASTEObjHandlers());
  419. #else
  420. #if WASTE_OBJECTS
  421.         if (newPICTUPP == NULL)
  422.         {
  423.             newPICTUPP = NewWENewObjectProc(HandleNewPicture);
  424.             disposePICTUPP = NewWEDisposeObjectProc(HandleDisposePicture);
  425.             drawPICTUPP = NewWEDrawObjectProc(HandleDrawPicture);
  426.         
  427.             newSndUPP = NewWENewObjectProc(HandleNewSound);
  428.             disposeSndUPP = NewWEDisposeObjectProc(HandleDisposeSound);
  429.             drawSndUPP = NewWEDrawObjectProc(HandleDrawSound);
  430.             clickSndUPP = NewWEClickObjectProc(HandleClickSound);
  431.         }
  432.         
  433.         // •• Install Picture Handlers ••
  434.         FailOSErr(WEInstallObjectHandler(kTypePicture, weNewHandler, (UniversalProcPtr)newPICTUPP, NULL));
  435.         FailOSErr(WEInstallObjectHandler(kTypePicture, weDisposeHandler, (UniversalProcPtr)disposePICTUPP, NULL));
  436.         FailOSErr(WEInstallObjectHandler(kTypePicture, weDrawHandler, (UniversalProcPtr)drawPICTUPP, NULL));
  437.     
  438.         // •• Install Sound Handlers ••
  439.         FailOSErr(WEInstallObjectHandler(kTypeSound, weNewHandler, (UniversalProcPtr)newSndUPP, NULL));
  440.         FailOSErr(WEInstallObjectHandler(kTypeSound, weDisposeHandler, (UniversalProcPtr)disposeSndUPP, NULL));
  441.         FailOSErr(WEInstallObjectHandler(kTypeSound, weDrawHandler, (UniversalProcPtr)drawSndUPP, NULL));
  442.         FailOSErr(WEInstallObjectHandler(kTypeSound, weClickHandler, (UniversalProcPtr)clickSndUPP, NULL));
  443. #endif // WASTE_OBJECTS
  444. #endif // WASTE_OBJECT_ARCHIVE
  445.     }
  446. #endif // WASTE 1.1
  447. }
  448.  
  449. #if WASTE_TABS
  450. /********************************************************\
  451.  InstallTabHandlers -- with WASTE 1.1 or later, will
  452.      install the hooks to support tabs in WASTE.  If
  453.      WASTE_AUTO_TABS, this will be called for you.
  454.      Newest version of WEInstallTabHooks returns an OSErr
  455.      but we'll ignore it for compatibility with the older
  456.      version
  457. \********************************************************/
  458.  
  459. void CWASTEText::InstallTabHandlers(void)
  460. {
  461.     WEInstallTabHooks(macWE);
  462. }
  463. #endif
  464.  
  465. /********************************************************\
  466.  SetTextMargin -- set a margin that will be left between
  467.     the destRect and viewRect in WASTE
  468. \********************************************************/
  469.  
  470. void CWASTEText::SetTextMargin(short margin)
  471. {
  472.     textMargin = margin;
  473.     CalcWERects();
  474. }
  475.  
  476. #if WASTE_VERSION >= 0x01100000
  477. /********************************************************\
  478.  WASTETrackingHandler - handles drag tracking
  479. \********************************************************/
  480.  
  481. pascal OSErr WASTETrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow,
  482.     void *handlerRefCon, DragReference theDrag)
  483. {
  484.     if (theWindow!=NULL)
  485.     {
  486.         ((CWASTEText *)handlerRefCon)->Prepare();
  487.         ClipRect(&theWindow->portRect);
  488.         return WETrackDrag(theMessage, theDrag, ((CWASTEText *)handlerRefCon)->macWE);    
  489.     }
  490.     else
  491.     {
  492.         return noErr;
  493.     }
  494. }
  495.  
  496. /********************************************************\
  497.  WASTEReceiveHandler - handles receiving a drag
  498. \********************************************************/
  499.  
  500. pascal OSErr WASTEReceiveHandler(WindowPtr theWindow, void *handlerRefCon, DragReference theDrag)
  501. {
  502.     OSErr err = noErr;
  503.     CWASTETask *wasteTask;
  504.     
  505.     if (theWindow!=NULL)
  506.     {
  507.         ((CWASTEText *)handlerRefCon)->Prepare();
  508.         ClipRect(&theWindow->portRect);
  509.         err = WEReceiveDrag(theDrag, ((CWASTEText *)handlerRefCon)->macWE);    
  510.         ((CWASTEText *)handlerRefCon)->AdjustBounds();
  511.         if (((CWASTEText *)handlerRefCon)->hasUndoSupport)
  512.         {
  513. #if TCL_VERSION >= 0x02000000
  514.             wasteTask = new CWASTETask((CWASTEText *)handlerRefCon);
  515. #else
  516.             wasteTask = new (CWASTETask);
  517.             wasteTask->IWASTETask((CWASTEText *)handlerRefCon);
  518. #endif
  519.             ((CWASTEText *)handlerRefCon)->itsSupervisor->Notify(wasteTask);
  520.         }
  521.     }
  522.     return err;
  523. }
  524.  
  525. #if WASTE_OBJECTS && !WASTE_OBJECT_ARCHIVE
  526.  
  527. /********************************************************\
  528.  HandleNewPicture - Handler for creating pictures
  529.      from WASTE Demo source
  530. \********************************************************/
  531.  
  532. pascal OSErr HandleNewPicture(Point *defaultObjectSize, WEObjectReference objectRef)
  533. {
  534.     PicHandle thePicture;
  535.     Rect frame;
  536.  
  537.     // { get handle to object data (in this case, a picture handle) }
  538.     thePicture = (PicHandle)WEGetObjectDataHandle(objectRef);
  539.  
  540.     // { figure out the default object size by looking at the picFrame record }
  541.     frame = (*thePicture)->picFrame;
  542.     OffsetRect(&frame, -frame.left, -frame.top);
  543.     defaultObjectSize->h = frame.right;
  544.     defaultObjectSize->v = frame.bottom;
  545.  
  546.     return noErr;
  547. }
  548.  
  549. /********************************************************\
  550.  HandleDisposePicture - handles disposing a picture
  551.      From WASTE Demo source
  552. \********************************************************/
  553.  
  554. pascal OSErr HandleDisposePicture(WEObjectReference objectRef)
  555. {
  556.     PicHandle thePicture;
  557.  
  558.     // { get handle to object data (in this case, a picture handle) }
  559.     thePicture = (PicHandle)WEGetObjectDataHandle(objectRef);
  560.  
  561.     // { kill the picture }
  562.     KillPicture(thePicture);
  563.  
  564.     return MemError();
  565. }
  566.  
  567. /********************************************************\
  568.  HandleDrawPicture - handle drawing a picture
  569.      From WASTE Demo source
  570. \********************************************************/
  571.  
  572. pascal OSErr HandleDrawPicture(Rect *destRect, WEObjectReference objectRef)
  573. {
  574.     PicHandle thePicture;
  575.  
  576.     // { get handle to object data (in this case, a picture handle) }
  577.     thePicture = (PicHandle)WEGetObjectDataHandle(objectRef);
  578.  
  579.     // { draw the picture }
  580.     DrawPicture(thePicture, destRect);
  581.  
  582.     return noErr;
  583. }
  584.  
  585. /********************************************************\
  586.  HandleNewSound - Handler for creating sounds
  587. \********************************************************/
  588.  
  589. pascal OSErr HandleNewSound(Point *defaultObjectSize, WEObjectReference objectRef)
  590. {
  591.     // icon is 32x32
  592.  
  593.     defaultObjectSize->h = 32;
  594.     defaultObjectSize->v = 32;
  595.  
  596.     return noErr;
  597. }
  598.  
  599. /********************************************************\
  600.  HandleDisposeSound - handles disposing a sound
  601. \********************************************************/
  602.  
  603. pascal OSErr HandleDisposeSound(WEObjectReference objectRef)
  604. {
  605.     Handle theSound;
  606.  
  607.     // { get handle to object data (in this case, a sound handle) }
  608.     theSound = WEGetObjectDataHandle(objectRef);
  609.  
  610.     DisposeHandle(theSound);
  611.  
  612.     return MemError();
  613. }
  614.  
  615. /********************************************************\
  616.  HandleClickSound - handle clicking a sound (play it)
  617. \********************************************************/
  618.  
  619. pascal OSErr HandleClickSound(Point hitPt, short modifiers, long clickTime,
  620.     WEObjectReference objectRef)
  621. {
  622.     OSErr err;
  623.     Handle theSound;
  624.  
  625.     // { get handle to object data (in this case, a sound handle) }
  626.     theSound = WEGetObjectDataHandle(objectRef);
  627.  
  628.     // the following is an attempt to check for Universal Headers 2.0
  629. #if !defined(GENERATINGPOWERPC) && !defined(GENERATING68K)
  630.     err = SndPlay(NULL, theSound, false);
  631. #else
  632.     err = SndPlay(NULL, (SndListHandle)theSound, false);
  633. #endif
  634.  
  635.     return err;
  636. }
  637.  
  638. /********************************************************\
  639.  HandleDrawSound -- draw an icon for the sound
  640. \********************************************************/
  641.  
  642. pascal OSErr HandleDrawSound(Rect *destRect, WEObjectReference objectRef)
  643. {
  644.     PlotIconID(destRect, atNone, ttNone, kSoundIconNumber);
  645.     return noErr;
  646. }
  647.  
  648. #endif // WASTE_OBJECTS && !WASTE_OBJECT_ARCHIVE
  649.  
  650. /********************************************************\
  651.  InstallDragHandlers - installs the handlers for
  652.      receiving drags.
  653.      Note: This can only be called for one CWASTEText
  654.      instance per window
  655. \********************************************************/
  656.  
  657. void CWASTEText::InstallDragHandlers(void)
  658. {
  659.     WindowPtr dragWindow = GetWindow()->macPort;
  660.  
  661.     if (gHasDragAndDrop && !dragHandlersInstalled)
  662.     {
  663.         if (trackingUPP == NULL) // need to create UPP
  664.         {
  665.             trackingUPP = NewDragTrackingHandlerProc(WASTETrackingHandler);
  666.         }
  667.         FailOSErr(InstallTrackingHandler(trackingUPP, dragWindow, this));
  668.  
  669.         if (receiveUPP == NULL) // need to create UPP
  670.         {
  671.             receiveUPP = NewDragReceiveHandlerProc(WASTEReceiveHandler);
  672.         }
  673.         FailOSErr(InstallReceiveHandler(receiveUPP, dragWindow, this));
  674.  
  675.         dragHandlersInstalled = true;
  676.     }
  677. }
  678.  
  679. /********************************************************\
  680.  RemoveDragHandlers -- removes the drag handlers
  681. \********************************************************/
  682.  
  683. void CWASTEText::RemoveDragHandlers(void)
  684. {
  685.     WindowPtr dragWindow = GetWindow()->macPort;
  686.  
  687.     if (gHasDragAndDrop && dragHandlersInstalled)
  688.     {
  689.         FailOSErr(RemoveTrackingHandler(trackingUPP, dragWindow));
  690.         FailOSErr(RemoveReceiveHandler(receiveUPP, dragWindow));
  691.         dragHandlersInstalled = false;
  692.     }
  693. }
  694. #endif // WASTE 1.1
  695.  
  696. /********************************************************\
  697.  CheckInsertion - sees if the specified amount of text
  698.      can be inserted
  699. \********************************************************/
  700.  
  701. void CWASTEText::CheckInsertion(long numChars, long styleSize, Boolean useSelection)
  702. {
  703.     long    selStart, selEnd;
  704.     long    growSize = numChars;
  705.     Handle    h;
  706.     OSErr    err;
  707.  
  708.     if (useSelection)
  709.     {
  710.         GetSelection(&selStart, &selEnd);
  711.         growSize -= TCLAbs(selEnd - selStart);
  712.     }
  713.  
  714.     /*    The problem with WASTE is that it can succeed in adding the text to its content
  715.      *    structure, and then fail to expand its style tables or line start tables.  The way
  716.      *    I address this is to add a fudge factor to the amount of data being added, and
  717.      *    hope that its enough to cover the WASTE overhead.
  718.      */
  719.  
  720.     if (growSize > 0)
  721.     {
  722.         /*    Add fudge factor for new line starts (1/5th of numChars)    */
  723.         
  724.         growSize += growSize / 5;
  725.         
  726.         /*    Add fudge factor for style information (1/2 of styleSize)    */
  727.  
  728.         growSize += styleSize + styleSize / 2;
  729.  
  730.         if (WEFeatureFlag(weFUseTempMem, weBitTest, macWE))
  731.         {
  732.             h = TempNewHandle(growSize, &err);
  733.             if (!h && err == noErr)
  734.                 err = memFullErr;
  735.             DisposeHandle(h);
  736.             FailOSErr(err);
  737.         }
  738.         else
  739.         {
  740.             h = NewHandleCanFail(growSize);
  741.             FailNIL(h);
  742.             DisposeHandle(h);
  743.         }
  744.     }
  745. }
  746.  
  747. #if WASTE_VERSION >= 0x01100000
  748. static long sOldModCount = -1; // for keeping track of modifications by inline input
  749. #endif
  750.  
  751. /********************************************************\
  752.  WEPreUpdate -- prepare the port
  753. \********************************************************/
  754.  
  755. pascal void CWASTEText::WEPreUpdate(WEHandle hWE)
  756. {
  757.     CWASTEText *itsOwner = NULL;
  758.  
  759.     WEGetInfo(weRefCon, (Ptr)&itsOwner, hWE);
  760. #if TCL_VERSION >= 0x02000000
  761.     TCL_ASSERT_OBJECT_PTR(itsOwner);
  762.     TCL_ASSERT(member(itsOwner, CWASTEText));
  763. #else
  764.     ASSERT(member(itsOwner, CWASTEText));
  765. #endif
  766.     
  767.     if (itsOwner != NULL)
  768.     {
  769. #if WASTE_VERSION >= 0x01100000
  770.         sOldModCount = WEGetModCount(hWE);
  771. #endif
  772.         itsOwner->Prepare();
  773.     }
  774. }
  775.  
  776. /********************************************************\
  777.  WEPostUpdate -- keep typing visible -- by M. Sladok
  778. \********************************************************/
  779.  
  780. pascal void CWASTEText::WEPostUpdate(WEHandle hWE, long fixLength, long inputAreaStart,
  781.     long inputAreaEnd, long pinRangeStart, long pinRangeEnd)
  782. {
  783. #if WASTE_VERSION >= 0x01100000
  784.     long newModCount;
  785.     CWASTETask *wasteTask;
  786. #endif
  787.     CWASTEText *itsOwner = NULL;
  788.  
  789.     WEGetInfo(weRefCon, (Ptr)&itsOwner, hWE);
  790. #if TCL_VERSION >= 0x02000000
  791.     TCL_ASSERT_OBJECT_PTR(itsOwner);
  792.     TCL_ASSERT(member(itsOwner, CWASTEText));
  793. #else
  794.     ASSERT(member(itsOwner, CWASTEText));
  795. #endif
  796.     
  797.     if (itsOwner != NULL)
  798.     {
  799.         itsOwner->AdjustBounds();
  800.         itsOwner->ScrollToSelection();
  801. #if WASTE_VERSION >= 0x01100000
  802.         newModCount = WEGetModCount(hWE);
  803.         if (itsOwner->hasUndoSupport && newModCount != sOldModCount) // inline input occured
  804.         {
  805. #if TCL_VERSION >= 0x02000000
  806.             wasteTask = new CWASTETask(itsOwner);
  807. #else
  808.             wasteTask = new (CWASTETask);
  809.             wasteTask->IWASTETask(itsOwner);
  810. #endif
  811.             itsOwner->itsSupervisor->Notify(wasteTask);
  812.         }
  813. #endif
  814.     }
  815. }
  816.  
  817. /********************************************************\
  818.  DoClick - handle mouse clicks
  819. \********************************************************/
  820.  
  821. void CWASTEText::DoClick(Point hitPt, short modifierKeys, long when)
  822. {
  823. #if WASTE_VERSION >= 0x01100000
  824.     long oldModCount, newModCount;
  825.     CWASTETask *wasteTask;
  826.  
  827.     if (!wantsClicks) return;
  828.     
  829.     oldModCount = WEGetModCount(macWE);
  830. #endif
  831.  
  832.     WEClick(hitPt, modifierKeys, when, macWE);
  833. #if WASTE_VERSION >= 0x01100000
  834.     newModCount = WEGetModCount(macWE);
  835. #endif
  836.     
  837.         // CSwitchboard will never see the mouse up that ended
  838.         // the drag, so we stuff gLastMouseUp here to allow
  839.         // multi-click counting to work.
  840.         
  841.     gLastMouseUp.what = mouseUp;
  842.     gLastMouseUp.when = TickCount();
  843.     gLastMouseUp.where = hitPt;
  844.     LocalToGlobal( &gLastMouseUp.where);
  845.     gLastMouseUp.modifiers = modifierKeys;
  846.  
  847.     SelectionChanged();
  848.     
  849.     // make task if a drag occured (so it can be undone)
  850. #if WASTE_VERSION >= 0x01100000
  851.     if (hasUndoSupport && oldModCount != newModCount) // drag occured
  852.     {
  853. #if TCL_VERSION >= 0x02000000
  854.         wasteTask = new CWASTETask(this);
  855. #else
  856.         wasteTask = new (CWASTETask);
  857.         wasteTask->IWASTETask(this);
  858. #endif
  859.         itsSupervisor->Notify(wasteTask);
  860.     }
  861. #endif
  862. #if WASTE_VERSION == 0x01000000
  863.     if (!editable && (gGopher == this))
  864.     {
  865.         long selStart, selEnd;
  866.         
  867.         GetSelection( &selStart, &selEnd);    
  868.         if (selStart == selEnd)
  869.             itsSupervisor->BecomeGopher( TRUE);
  870.     }
  871. #endif
  872. }
  873.  
  874. #if WASTE_VERSION >= 0x01100000
  875.  
  876. /******************************************************************************
  877.  DoKeyDown - handles key strokes and the undoing of them
  878.      This version is for WASTE 1.1 or higher
  879.  ******************************************************************************/
  880.  
  881. void CWASTEText::DoKeyDown(char theChar, Byte keyCode, EventRecord *macEvent)
  882. {
  883.     CWASTETask *wasteTask;
  884.  
  885.     // Don't try to insert and command keys into the text,
  886.     // pass them along the command chain
  887.  
  888.     if (macEvent->modifiers & cmdKey)
  889.     {
  890.         inherited::DoKeyDown(theChar, keyCode, macEvent);
  891.         return;
  892.     }
  893.  
  894.     switch (keyCode)
  895.     {
  896.  
  897.         case KeyHome:
  898.         case KeyPageUp:
  899.         case KeyPageDown:
  900.         case KeyEnd:
  901.         case KeyLeftCursor:
  902.         case KeyRightCursor:
  903.         case KeyUpCursor:
  904.         case KeyDownCursor:
  905.             inherited::DoKeyDown(theChar, keyCode, macEvent);
  906.             break;
  907.  
  908.         default:
  909.             if (editable)
  910.             {
  911.                 Boolean wasTyping = WEIsTyping(macWE);
  912.                 
  913.                 TypeChar(theChar, macEvent->modifiers);
  914.                         if (hasUndoSupport && !wasTyping)
  915.                         {
  916. #if TCL_VERSION >= 0x02000000
  917.                     wasteTask = new CWASTETask(this);
  918. #else
  919.                     wasteTask = new (CWASTETask);
  920.                     wasteTask->IWASTETask(this);
  921. #endif
  922.                     itsSupervisor->Notify(wasteTask);
  923.                 }
  924.             }
  925.             break;
  926.     }
  927. }
  928.  
  929. /********************************************************\
  930.  DoCommand - version that uses WASTE 1.1's undo features
  931. \********************************************************/
  932.  
  933. void CWASTEText::DoCommand(long theCommand)
  934. {
  935.     CWASTETask *wasteTask;
  936.     Boolean makeTask = false;
  937.     Str255            itemName;
  938.     long            number;
  939.  
  940.     if (theCommand < 0)
  941.     {
  942. #if TCL_VERSION >= 0x02000000
  943.         switch (TCLHiShort(-theCommand))
  944. #else
  945.         switch (HiShort(-theCommand))
  946. #endif
  947.         {
  948.             // •• font menu ••
  949.             case MENUfont:
  950.                 gBartender->GetCmdText(theCommand, itemName);
  951.                 SetFontName(itemName);
  952.                 makeTask = true;
  953.                 break;
  954.             
  955.             // •• Size menu ••
  956.             case MENUsize:
  957.                 gBartender->GetCmdText(theCommand, itemName);
  958.                 StringToNum(itemName, &number);
  959.                 SetFontSize(number);
  960.                 makeTask = true;
  961.                 break;
  962.  
  963.             default:
  964.                 CPanorama::DoCommand(theCommand);
  965.                 break;
  966.         }
  967.  
  968.     }
  969.     else
  970.     {
  971.         switch (theCommand)
  972.         {
  973.             case cmdCut:
  974.             case cmdPaste:
  975.             case cmdClear:
  976.                 if (!editable)
  977.                 {
  978.                     SysBeep(3);
  979.                     break;
  980.                 }
  981.                 PerformEditCommand(theCommand);
  982.                 makeTask = true;
  983.                 break;
  984.  
  985.             case cmdCopy:
  986.                 PerformEditCommand(theCommand);
  987.                 break;
  988.  
  989.             case cmdSelectAll:
  990.                 Prepare();
  991.                 SelectAll(TRUE);
  992.                 SelectionChanged();
  993.                 break;
  994.  
  995.             case cmdPlain:
  996.                 SetFontStyle(NOTHING);
  997.                 makeTask = true;
  998.                 break;
  999.  
  1000.             case cmdBold:                    // Styles are set by flipping bits
  1001.             case cmdItalic:                    //   Bold is bit 0, Italic is bit 1
  1002.             case cmdUnderline:                //   Underline is bit 2, etc.
  1003.             case cmdOutline:                //   By cleverly numbering these
  1004.             case cmdShadow:                    //   commands, we can just shift
  1005.             case cmdCondense:                //   a bit to get the proper style
  1006.             case cmdExtend:
  1007.                 SetFontStyle(1 << (theCommand - cmdBold));
  1008.                 makeTask = true;
  1009.                 break;
  1010.  
  1011.             case cmdBlack:
  1012.                     SetFontColor(&RGBBlack);
  1013.                     makeTask = true;
  1014.                     break;
  1015.             case cmdRed:
  1016.                     SetFontColor(&RGBRed);
  1017.                     makeTask = true;
  1018.                     break;
  1019.             case cmdGreen:
  1020.                     SetFontColor(&RGBGreen);
  1021.                     makeTask = true;
  1022.                     break;
  1023.             case cmdBlue:
  1024.                     SetFontColor(&RGBBlue);
  1025.                     makeTask = true;
  1026.                     break;
  1027.             case cmdCyan:
  1028.                     SetFontColor(&RGBCyan);
  1029.                     makeTask = true;
  1030.                     break;
  1031.             case cmdMagenta:
  1032.                     SetFontColor(&RGBMagenta);
  1033.                     makeTask = true;
  1034.                     break;
  1035.             case cmdYellow:
  1036.                     SetFontColor(&RGBYellow);
  1037.                     makeTask = true;
  1038.                     break;
  1039.  
  1040.             case cmdAlignLeft:
  1041.             case cmdAlignCenter:
  1042.             case cmdAlignRight:
  1043.             case cmdJustify:
  1044.                 SetAlignCmd(theCommand);
  1045.                 // not undoable at this time
  1046.                 break;
  1047.  
  1048.             case cmdSingleSpace:
  1049.             case cmd1HalfSpace:
  1050.             case cmdDoubleSpace:
  1051.                 SetSpacingCmd(theCommand);
  1052.                 // not undoable at this time
  1053.                 break;
  1054.  
  1055.             default:
  1056.                 CPanorama::DoCommand(theCommand);
  1057.                 break;
  1058.         }
  1059.     }
  1060.     
  1061.     if (makeTask && hasUndoSupport)
  1062.     {
  1063. #if TCL_VERSION >= 0x02000000
  1064.         wasteTask = new CWASTETask(this);
  1065. #else
  1066.         wasteTask = new (CWASTETask);
  1067.         wasteTask->IWASTETask(this);
  1068. #endif
  1069.         itsSupervisor->Notify(wasteTask);
  1070.     }
  1071. }
  1072.  
  1073. #else // WASTE 1.0
  1074. /********************************************************\
  1075.  DoCommand - AbstractText does not deal with justify so
  1076.      we have to special case it. And color too
  1077.      This version is for WASTE 1.0
  1078. \********************************************************/
  1079.  
  1080. void    CWASTEText::DoCommand(long theCommand)
  1081. {
  1082.     CTextStyleTask    *styleTask = NULL;
  1083.     Boolean        makeStyleTask = FALSE;
  1084.  
  1085.     if (stylable)
  1086.     {
  1087.         switch (theCommand)
  1088.         {
  1089.             case cmdJustify:
  1090.             case cmdBlack:
  1091.             case cmdRed:
  1092.             case cmdBlue:
  1093.             case cmdGreen:
  1094.             case cmdCyan:
  1095.             case cmdMagenta:
  1096.             case cmdYellow:
  1097.                 makeStyleTask =TRUE;
  1098.                 break;
  1099.             default: ;
  1100.         }
  1101.     }
  1102.         
  1103.     if (makeStyleTask)
  1104.     {
  1105.         itsTypingTask = NULL;
  1106.         styleTask = MakeStyleTask(theCommand);
  1107.         itsLastTask = styleTask;
  1108.         itsSupervisor->Notify(styleTask);
  1109.         styleTask->Do();
  1110.     }
  1111.     else
  1112.         inherited::DoCommand(theCommand);
  1113. }
  1114. #endif
  1115.  
  1116. /********************************************************\
  1117.  UpdateMenus - handle WASTE specific menu enabling
  1118. \********************************************************/
  1119.  
  1120. void CWASTEText::UpdateMenus()
  1121. {
  1122.     long    selStart, selEnd;
  1123.     TextStyle        style;
  1124.     short            styleFlags;
  1125.  
  1126.     inherited::UpdateMenus();
  1127.     
  1128.         // Copy and Cut are only possible if the selection is small (< 32K).
  1129.         // The danger here is that a piece of styled text > 32K will pass
  1130.         // through the clipboard to another applciation which cannot handle
  1131.         // it (like the Finder).
  1132.         //
  1133.         // Should probably override DoCommand as well to make sure a command
  1134.         // does not get past the Menu Manager -- can be done later.
  1135.     
  1136.     GetSelection(&selStart, &selEnd);
  1137.     if (TCLAbs(selEnd - selStart) >= 32767)
  1138.     {
  1139.         gBartender->DisableCmd(cmdCopy);
  1140.         gBartender->DisableCmd(cmdCut);
  1141.     }
  1142. #if WASTE_VERSION >= 0x01100000
  1143.     if (WECanPaste(macWE))
  1144.         gBartender->EnableCmd(cmdPaste);
  1145.     else
  1146.         gBartender->DisableCmd(cmdPaste);
  1147. #endif
  1148.  
  1149.     gBartender->EnableCmd(cmdBlack);
  1150.     gBartender->EnableCmd(cmdRed);
  1151.     gBartender->EnableCmd(cmdGreen);
  1152.     gBartender->EnableCmd(cmdBlue);
  1153.     gBartender->EnableCmd(cmdCyan);
  1154.     gBartender->EnableCmd(cmdMagenta);
  1155.     gBartender->EnableCmd(cmdYellow);
  1156.  
  1157.     // figure out the continuous color
  1158.     styleFlags = doColor;
  1159.     GetTextStyle (&styleFlags,  &style);
  1160.  
  1161.     if (styleFlags & doColor)
  1162.     {
  1163.         if (style.tsColor.red == 0)
  1164.         {
  1165.             if (style.tsColor.green == 0)
  1166.             {
  1167.                 if (style.tsColor.blue == 0)
  1168.                 {
  1169.                     gBartender->CheckMarkCmd(cmdBlack, true);
  1170.                 }
  1171.                 else if (style.tsColor.blue == 65535)
  1172.                 {
  1173.                     gBartender->CheckMarkCmd(cmdBlue, true);
  1174.                 }
  1175.             }
  1176.             else if (style.tsColor.green == 65535)
  1177.             {
  1178.                 if (style.tsColor.blue == 0)
  1179.                 {
  1180.                     gBartender->CheckMarkCmd(cmdGreen, true);
  1181.                 }
  1182.                 else if (style.tsColor.blue == 65535)
  1183.                 {
  1184.                     gBartender->CheckMarkCmd(cmdCyan, true);
  1185.                 }
  1186.             }
  1187.         }
  1188.         else if (style.tsColor.red == 65535)
  1189.         {
  1190.             if (style.tsColor.green == 0)
  1191.             {
  1192.                 if (style.tsColor.blue == 0)
  1193.                 {
  1194.                     gBartender->CheckMarkCmd(cmdRed, true);
  1195.                 }
  1196.                 else if (style.tsColor.blue == 65535)
  1197.                 {
  1198.                     gBartender->CheckMarkCmd(cmdMagenta, true);
  1199.                 }
  1200.             }
  1201.             else if (style.tsColor.green == 65535)
  1202.             {
  1203.                 if (style.tsColor.blue == 0)
  1204.                 {
  1205.                     gBartender->CheckMarkCmd(cmdYellow, true);
  1206.                 }
  1207.                 else if (style.tsColor.blue == 65535)
  1208.                 {
  1209.                     // no white
  1210.                 }
  1211.             }
  1212.         }
  1213.     }
  1214.     
  1215. }
  1216.  
  1217. #if WASTE_VERSION == 0x01000000
  1218. /********************************************************\
  1219.  MakeEditTask -- use WASTE specific one
  1220.      For WASTE 1.0 only
  1221. \********************************************************/
  1222.  
  1223. CTextEditTask *CWASTEText::MakeEditTask( long editCmd)
  1224. {
  1225.     CWASTEEditTask *volatile editTask = NULL;
  1226.     
  1227.     try_
  1228.     {
  1229. #if TCL_VERSION >= 0x02000000
  1230.         editTask = new CWASTEEditTask(this, editCmd, cFirstTaskIndex);
  1231. #else
  1232.         editTask = new (CWASTEEditTask);
  1233.         editTask->IWASTEEditTask(this, editCmd, cFirstTaskIndex);
  1234. #endif
  1235.     }
  1236.     catch_all_()
  1237.     {
  1238. #if TCL_VERSION >= 0x02000000
  1239.         TCLForgetObject(editTask);
  1240. #else
  1241.         ForgetObject(editTask);
  1242. #endif
  1243.  
  1244.         throw_same_();
  1245.     }
  1246.     end_try_
  1247.     
  1248.     return editTask;
  1249. }
  1250.  
  1251. /********************************************************\
  1252.  MakeStyleTask -- use WASTE specific one
  1253.      WASTE 1.0 only
  1254. \********************************************************/
  1255.  
  1256. CTextStyleTask *CWASTEText::MakeStyleTask( long styleCmd)
  1257. {
  1258.  
  1259.     CWASTEStyleTask *volatile newTask = NULL;
  1260.     short            taskIndex;
  1261.     
  1262.     try_
  1263.     {
  1264.         taskIndex = cFirstTaskIndex > 0 ? cFirstTaskIndex + undoFormatting : 0;
  1265. #if TCL_VERSION >= 0x02000000
  1266.         newTask = new CWASTEStyleTask(this, styleCmd, taskIndex);
  1267. #else
  1268.         newTask = new (CWASTEStyleTask);
  1269.         newTask->IWASTEStyleTask(this, styleCmd, taskIndex);
  1270. #endif
  1271.     }
  1272.  
  1273.     catch_all_()
  1274.     {
  1275. #if TCL_VERSION >= 0x02000000
  1276.         TCLForgetObject( newTask);
  1277. #else
  1278.         ForgetObject(newTask);
  1279. #endif
  1280.  
  1281.         throw_same_();
  1282.     }
  1283.     end_try_
  1284.     
  1285.     return newTask;
  1286. }
  1287. #endif
  1288.  
  1289. /********************************************************\
  1290.  PerformEditCommand - handle cut, copy, paste, and clear
  1291. \********************************************************/
  1292.  
  1293. void CWASTEText::PerformEditCommand(long theCommand)
  1294. {
  1295.     Boolean        saveAllocState;
  1296.     OSErr        err = noErr;
  1297.  
  1298.     Prepare();
  1299.     if (!ReallyVisible())
  1300.         SetOrigin(-10000, -10000);
  1301.  
  1302.     switch( theCommand)
  1303.     {        
  1304.         case cmdCut:
  1305.             gClipboard->EmptyGlobalScrap();
  1306.             saveAllocState = SetAllocation(kAllocCanFail);        
  1307.             err = WECut(macWE);
  1308.             SetAllocation(saveAllocState);
  1309.             if (err == noErr)
  1310.                 gClipboard->UpdateDisplay();
  1311.             break;
  1312.             
  1313.         case cmdCopy:
  1314.             gClipboard->EmptyGlobalScrap();
  1315.             saveAllocState = SetAllocation(kAllocCanFail);        
  1316.             err = WECopy(macWE);
  1317.             SetAllocation(saveAllocState);
  1318.             if (err == noErr)
  1319.                 gClipboard->UpdateDisplay();
  1320.             break;
  1321.             
  1322.         case cmdPaste:
  1323.             // note: doesn't check the soup...
  1324.             CheckInsertion(gClipboard->DataSize('TEXT'),
  1325.                            gClipboard->DataSize('styl'), TRUE);
  1326.             saveAllocState = SetAllocation(kAllocCanFail);
  1327.             err = WEPaste(macWE);
  1328.             SetAllocation(saveAllocState);
  1329.             break;
  1330.             
  1331.         case cmdClear:
  1332.             saveAllocState = SetAllocation(kAllocCanFail);        
  1333.             err = WEDelete(macWE);
  1334.             SetAllocation(saveAllocState);
  1335.             break;
  1336.             
  1337.     }
  1338.     AdjustBounds();
  1339.     ScrollToSelection();
  1340.     FailOSErr(err);
  1341. }
  1342.  
  1343.  
  1344. /********************************************************\
  1345.  Draw - draw the text
  1346. \********************************************************/
  1347.  
  1348. void CWASTEText::Draw(Rect *area)
  1349. {
  1350.     RgnHandle    updateRgn;
  1351.     LongRect    fr, dr;
  1352.     Rect        r;
  1353.     short        oldOffscreen, oldOutline;
  1354.     OSErr        err;
  1355.     
  1356.     if (!ReallyVisible())
  1357.         return;
  1358.  
  1359.     updateRgn = NewRgn();
  1360.     RectRgn(updateRgn, area);
  1361.     
  1362.     if (!printing)
  1363.     {
  1364.         SectRgn(updateRgn, macPort->visRgn, updateRgn);
  1365.         CalcWERects();
  1366.     }
  1367.     else
  1368.     {
  1369.         err = WESetInfo(wePort, (Ptr)&qd.thePort, macWE);
  1370.         // temporarily turn off off screen drawing and outline hiliting
  1371.         oldOffscreen = WEFeatureFlag(weFDrawOffscreen, weBitClear, macWE);
  1372.         oldOutline = WEFeatureFlag(weFOutlineHilite, weBitClear, macWE);
  1373.  
  1374.         // Prepare the destination and view rectangles
  1375.  
  1376.         WEGetDestRect(&dr, macWE);
  1377.         GetAperture(&fr);
  1378.         OffsetLongRect(&dr, -(dr.left + fr.left), -(dr.top + fr.top));
  1379.         FrameToQDR(&fr, &r);
  1380.         OffsetLongRect(&dr, r.left, r.top);
  1381.         dr.right = (lineWidth > 0) ? dr.left + lineWidth : printPageWidth;
  1382.         WESetDestRect(&dr, macWE);
  1383.         QDToLongRect(area, &fr);
  1384.         if (wholeLines)
  1385.             fr.bottom = fr.top + vScale * ((area->bottom - area->top) / vScale);
  1386.         WESetViewRect(&fr, macWE);
  1387.     }
  1388.  
  1389.     WEUpdate(updateRgn, macWE);
  1390.     // not necessary -- err = WESetInfo(wePort, &macPort, macWE);
  1391.     DisposeRgn(updateRgn);
  1392.  
  1393.     if (printing)
  1394.     {
  1395.         err = WESetInfo(wePort, (Ptr)&macPort, macWE);
  1396.         WEFeatureFlag(weFDrawOffscreen, oldOffscreen, macWE);
  1397.         WEFeatureFlag(weFOutlineHilite, oldOutline, macWE);
  1398.     }
  1399. }
  1400.  
  1401.  
  1402. /********************************************************\
  1403.  Activate - activate the pane
  1404. \********************************************************/
  1405.  
  1406. void CWASTEText::Activate()
  1407. {
  1408. #if WASTE_VERSION == 0x01000000
  1409.     TSMDocumentID aTSMDocument;
  1410. #endif
  1411.  
  1412.     if (curWASTEText==this) // already active
  1413.     {
  1414.         return;
  1415.     }
  1416.     
  1417.     CAbstractText::Activate();
  1418.     
  1419.     // deactivate old WASTEText
  1420.     if (curWASTEText!=NULL) curWASTEText->Deactivate();
  1421.     Prepare();
  1422.     CalcWERects();
  1423.     WEActivate(macWE);
  1424.     curWASTEText = this;
  1425.  
  1426. #if WASTE_VERSION == 0x01000000
  1427.     if (!editable)
  1428.     {
  1429.         // if not editable, deactivate TSM so inline input won't work
  1430.         // WASTE 1.1 adds a readonly flag so we don't have to do this
  1431.         if (gUsingTSM)
  1432.         {
  1433.             if (WEGetInfo(weTSMDocumentID, (Ptr)&aTSMDocument, macWE) == noErr)
  1434.             {
  1435.                 if (aTSMDocument != NULL)
  1436.                     DeactivateTSMDocument(aTSMDocument);
  1437.             }
  1438.         }
  1439.     }
  1440. #endif
  1441. }
  1442.  
  1443. /********************************************************\
  1444.  Deactivate - deactivate the pane
  1445. \********************************************************/
  1446.  
  1447. void CWASTEText::Deactivate()
  1448. {
  1449.     CAbstractText::Deactivate();
  1450.  
  1451.     Prepare();
  1452.     WEStopInlineSession(macWE);
  1453.     if (macWE)
  1454.     {
  1455.         CalcWERects();
  1456.         WEDeactivate(macWE);
  1457.     }
  1458.     curWASTEText = NULL;
  1459. }
  1460.  
  1461. /********************************************************\
  1462.  SetSelection - set what text is selected
  1463. \********************************************************/
  1464.  
  1465. void CWASTEText::SetSelection(long selStart, long selEnd, Boolean fRedraw)
  1466. {
  1467.     short wasActive, wasOutline;
  1468.  
  1469.     Prepare();    
  1470.     if (!fRedraw)
  1471.     {
  1472.         InhibitRecal(true);
  1473.     }
  1474.     WESetSelection(selStart, selEnd, macWE);
  1475.     if (!fRedraw)
  1476.     {
  1477.         InhibitRecal(false);
  1478.     }
  1479. }
  1480.  
  1481. /********************************************************\
  1482.  Clear - clear the text
  1483.     Deactivates text before clearing to prevent
  1484.     highlighting from showing up
  1485. \********************************************************/
  1486.  
  1487. void CWASTEText::Clear(void)
  1488. {
  1489.     Boolean    saveAllocState;
  1490.     OSErr    err;
  1491. #if WASTE_VERSION >= 0x01100000
  1492.     Boolean wasReadOnly;
  1493. #endif
  1494.  
  1495.     Prepare();
  1496.     
  1497.     SetSelection(0, 0x7FFFFFFF, false);
  1498.     saveAllocState = SetAllocation(kAllocCanFail);
  1499. #if WASTE_VERSION >= 0x01100000
  1500.     wasReadOnly = WEFeatureFlag(weFReadOnly, weBitClear, macWE);
  1501. #endif
  1502.     err = WEDelete(macWE);
  1503. #if WASTE_VERSION >= 0x01100000
  1504.     WEFeatureFlag(weFReadOnly, wasReadOnly, macWE);
  1505. #endif
  1506.     SetAllocation(saveAllocState);
  1507.     
  1508.     AdjustBounds();
  1509.     ScrollToSelection();
  1510. //    itsSupervisor->Notify(NULL); // can't undo
  1511.     FailOSErr(err);
  1512.     
  1513.     Refresh();
  1514.  
  1515. }
  1516.  
  1517. /********************************************************\
  1518.  SetTextPtr - set the text to a block of memory
  1519. \********************************************************/
  1520.  
  1521. void CWASTEText::SetTextPtr(Ptr textPtr, long numChars)
  1522. {
  1523.     Boolean        saveAllocState;
  1524.     OSErr        err;
  1525. #if WASTE_VERSION >= 0x01100000
  1526.     Boolean        wasReadOnly;
  1527. #endif
  1528.  
  1529.     Prepare();
  1530.     if (!ReallyVisible())
  1531.         SetOrigin(-10000, -10000);
  1532.     
  1533.     SetSelection(0, 0x7FFFFFFF, false);
  1534. #if WASTE_VERSION >= 0x01100000
  1535.     wasReadOnly = WEFeatureFlag(weFReadOnly, weBitClear, macWE);
  1536. #endif
  1537.     FailOSErr(WEDelete(macWE));
  1538.     CheckInsertion(numChars, 0, FALSE);
  1539.     saveAllocState = SetAllocation(kAllocCanFail);
  1540. #if WASTE_VERSION >= 0x01100000
  1541.     err = WEInsert(textPtr, numChars, NULL, NULL, macWE);
  1542.     WEFeatureFlag(weFReadOnly, wasReadOnly, macWE);
  1543. #else
  1544.     err = WEInsert(textPtr, numChars, NULL, macWE);
  1545. #endif
  1546.     SetAllocation(saveAllocState);
  1547.  
  1548.     AdjustBounds();
  1549.     Refresh();
  1550.  
  1551. //    itsSupervisor->Notify(NULL); // can't undo
  1552.     
  1553.     FailOSErr(err);
  1554. }
  1555.  
  1556. /********************************************************\
  1557.  StopInlineSession - stop inline session, confirm text
  1558. \********************************************************/
  1559.  
  1560. void CWASTEText::StopInlineSession(void)
  1561. {
  1562.     WEStopInlineSession(macWE);
  1563. }
  1564.  
  1565. /********************************************************\
  1566.  SetOutlineHighliting - turns on or off outline
  1567.      highliting and returns old setting
  1568. \********************************************************/
  1569.  
  1570. Boolean CWASTEText::SetOutlineHighliting(Boolean hilite)
  1571. {
  1572.     Boolean oldValue;
  1573.     
  1574.     oldValue = (WEFeatureFlag(weFOutlineHilite, 
  1575.         hilite ? weBitSet : weBitClear, macWE) == weBitSet);
  1576.         
  1577.     return oldValue;
  1578. }
  1579.  
  1580. /********************************************************\
  1581.  GetTextHandle - get a handle to the text
  1582.      This is not a copy of the handle, but the real thing
  1583. \********************************************************/
  1584.  
  1585. Handle CWASTEText::GetTextHandle()
  1586. {
  1587.     return( (Handle) WEGetText(macWE));
  1588. }
  1589.  
  1590. /********************************************************\
  1591.  CopyTextRange - return a handle to a copy of the
  1592.      indicated range of text
  1593. \********************************************************/
  1594.  
  1595. Handle CWASTEText::CopyTextRange(long start, long end)
  1596. {
  1597.     Handle    h;
  1598.     long    len;
  1599.     
  1600. #if TCL_VERSION >= 0x02000000
  1601.     end = TCLMin(end, WEGetTextLength(macWE));
  1602.     len = TCLMax(end - start, 0);
  1603. #else
  1604.     end = Min(end, WEGetTextLength(macWE));
  1605.     len = Max(end - start, 0);
  1606. #endif
  1607.     h = NewHandleCanFail(len);
  1608.     FailNIL(h);
  1609.     if (len > 0)
  1610.         BlockMoveData( (char*)*(WEGetText(macWE)) + start, *h, len);
  1611.     
  1612.     return h;
  1613. }
  1614.  
  1615. /********************************************************\
  1616.  CopyRangeWithStyle - return a handle to the text and
  1617.      styles in a range.  Handles must be previously
  1618.      created with NewHandle();
  1619. \********************************************************/
  1620.  
  1621. void CWASTEText::CopyRangeWithStyle(long start, long end, Handle hText,
  1622.     StScrpHandle hStyles)
  1623. {
  1624.     Boolean        saveAllocState;
  1625.     OSErr        err;
  1626.     
  1627.     saveAllocState = SetAllocation(kAllocCanFail);
  1628. #if WASTE_VERSION >= 0x01100000
  1629.     err = WECopyRange(start, end, hText, hStyles, NULL, macWE);
  1630. #else
  1631.     err = WECopyRange(start, end, hText, hStyles, macWE);
  1632. #endif
  1633.     SetAllocation(saveAllocState);
  1634.     FailOSErr(err);
  1635. }
  1636.  
  1637. #if WASTE_VERSION >= 0x01100000
  1638. /********************************************************\
  1639.  CopyRangeWithStyleSoup - return a handle to the text and
  1640.      styles and soup in a range.  Handles must be previously
  1641.      created with NewHandle();
  1642. \********************************************************/
  1643.  
  1644. void CWASTEText::CopyRangeWithStyleSoup(long start, long end, Handle hText,
  1645.     StScrpHandle hStyles, WESoupHandle hSoup)
  1646. {
  1647.     Boolean        saveAllocState;
  1648.     OSErr        err;
  1649.     
  1650.     saveAllocState = SetAllocation(kAllocCanFail);
  1651.     err = WECopyRange(start, end, hText, hStyles, hSoup, macWE);
  1652.     SetAllocation(saveAllocState);
  1653.     FailOSErr(err);
  1654. }
  1655. #endif
  1656.  
  1657. /********************************************************\
  1658.  InsertTextPtr - insert a block of text
  1659. \********************************************************/
  1660.  
  1661. void CWASTEText::InsertTextPtr(Ptr text, long length, Boolean fRedraw)
  1662. {
  1663.     Boolean        saveAllocState;
  1664.     OSErr        err;
  1665.     Boolean        wasReadOnly;
  1666.  
  1667.     Prepare();
  1668.     if (!ReallyVisible())
  1669.         SetOrigin(-10000, -10000);
  1670.  
  1671.     CheckInsertion(length, 0, TRUE);
  1672.     saveAllocState = SetAllocation(kAllocCanFail);
  1673.  
  1674.     if (!fRedraw) InhibitRedraw(true);
  1675.  
  1676. #if WASTE_VERSION >= 0x01100000
  1677.     wasReadOnly = WEFeatureFlag(weFReadOnly, weBitClear, macWE);
  1678.  
  1679.     err = WEInsert(text, length, NULL, NULL, macWE);
  1680.     WEFeatureFlag(weFReadOnly, wasReadOnly, macWE);
  1681. #else
  1682.     err = WEInsert(text, length, NULL, macWE);
  1683. #endif
  1684.     SetAllocation(saveAllocState);
  1685.  
  1686.     AdjustBounds();
  1687.  
  1688. //    itsSupervisor->Notify(NULL); // can't undo
  1689.  
  1690.     if (!fRedraw) InhibitRedraw(false);
  1691.  
  1692.     FailOSErr(err);
  1693. }
  1694.  
  1695. /********************************************************\
  1696.  InsertWithStyle -- insert text along with style
  1697. \********************************************************/
  1698.  
  1699. void CWASTEText::InsertWithStyle(Ptr text, long length, StScrpHandle hStyles,
  1700.         Boolean fRedraw)
  1701. {
  1702.     Boolean        saveAllocState;
  1703.     OSErr        err;
  1704. #if WASTE_VERSION >= 0x01100000
  1705.     Boolean        wasReadOnly;
  1706. #else
  1707.     long        i, numStyles;
  1708.     StScrpPtr    styles;
  1709. #endif
  1710.  
  1711.     Prepare();
  1712.     if (!ReallyVisible())
  1713.         SetOrigin(-10000, -10000);
  1714.     CheckInsertion(length, hStyles ? GetHandleSize((Handle) hStyles) : 0, TRUE);
  1715.  
  1716.     if (!fRedraw) InhibitRedraw(true);
  1717.  
  1718. #if WASTE_VERSION == 0x01000000
  1719.     //    Fix style entries which have a font size of 0
  1720.     // newer versions of WASTE do this for us
  1721.     if (hStyles)
  1722.     {
  1723.         styles = *hStyles;
  1724.         
  1725.         numStyles = styles->scrpNStyles;
  1726.         for (i = 0; i < numStyles; i++)
  1727.             if (styles->scrpStyleTab[i].scrpSize == 0)
  1728.                 styles->scrpStyleTab[i].scrpSize = 12;
  1729.     }
  1730. #endif
  1731.  
  1732.     saveAllocState = SetAllocation(kAllocCanFail);
  1733. #if WASTE_VERSION >= 0x01100000
  1734.     wasReadOnly = WEFeatureFlag(weFReadOnly, weBitClear, macWE);
  1735.     err = WEInsert(text, length, hStyles, NULL, macWE);
  1736.     WEFeatureFlag(weFReadOnly, wasReadOnly, macWE);
  1737. #else
  1738.     err = WEInsert(text, length, hStyles, macWE);
  1739. #endif
  1740.     SetAllocation(saveAllocState);
  1741.  
  1742.     AdjustBounds();
  1743. //    itsSupervisor->Notify(NULL); // can't undo
  1744.  
  1745.     if (!fRedraw) InhibitRedraw(false);
  1746.  
  1747.     FailOSErr(err);
  1748. }
  1749.  
  1750. #if WASTE_VERSION >= 0x01100000
  1751. /********************************************************\
  1752.  InsertWithStyleSoup -- insert text along with style
  1753.     and soup
  1754. \********************************************************/
  1755.  
  1756. void CWASTEText::InsertWithStyleSoup(Ptr text, long length, StScrpHandle hStyles,
  1757.         WESoupHandle hSoup, Boolean fRedraw)
  1758. {
  1759.     Boolean        saveAllocState;
  1760.     OSErr        err;
  1761.     Boolean        wasReadOnly;
  1762.  
  1763.     Prepare();
  1764.     if (!ReallyVisible())
  1765.         SetOrigin(-10000, -10000);
  1766.     CheckInsertion(length, hStyles ? GetHandleSize((Handle) hStyles) : 0, TRUE);
  1767.  
  1768.     if (!fRedraw) InhibitRedraw(true);
  1769.  
  1770.     saveAllocState = SetAllocation(kAllocCanFail);
  1771.     wasReadOnly = WEFeatureFlag(weFReadOnly, weBitClear, macWE);
  1772.     err = WEInsert(text, length, hStyles, hSoup, macWE);
  1773.     WEFeatureFlag(weFReadOnly, wasReadOnly, macWE);
  1774.     SetAllocation(saveAllocState);
  1775.  
  1776.     AdjustBounds();
  1777.  
  1778. //    itsSupervisor->Notify(NULL); // can't undo
  1779.  
  1780.     if (!fRedraw) InhibitRedraw(false);
  1781.  
  1782.     FailOSErr(err);
  1783. }
  1784. #endif
  1785.  
  1786.  
  1787. /********************************************************\
  1788.  TypeChar - type a character
  1789. \********************************************************/
  1790.  
  1791. void CWASTEText::TypeChar(char theChar, short theModifers)
  1792. {
  1793.     Boolean        saveAllocState;
  1794.  
  1795.     Prepare();
  1796.  
  1797.     CheckInsertion(1, 0, TRUE);
  1798.     saveAllocState = SetAllocation(kAllocCanFail);
  1799.  
  1800.     WEKey(theChar, theModifers, macWE);
  1801.     SetAllocation(saveAllocState);
  1802.  
  1803.     AdjustBounds();
  1804.     ScrollToSelection();
  1805. }
  1806.  
  1807. /********************************************************\
  1808.  CalcWERects - Sets the DestRect and ViewRect fields
  1809.      used by WASTE
  1810. \********************************************************/
  1811.  
  1812. void CWASTEText::CalcWERects()
  1813. {
  1814.     LongRect    fr, dr;
  1815.     Rect        r;
  1816.  
  1817.         // Prepare the destination and view rectangles
  1818.  
  1819.     WEGetDestRect(&dr, macWE);
  1820.     OffsetLongRect(&dr, -(dr.left + frame.left), -(dr.top + frame.top));
  1821.     FrameToQDR(&frame, &r);
  1822.     OffsetLongRect(&dr, r.left, r.top);
  1823.     dr.right = (lineWidth > 0) ? dr.left + lineWidth : frame.right;
  1824.     dr.top += textMargin;
  1825.     dr.bottom -= textMargin;
  1826.     dr.left += textMargin;
  1827.     dr.right -= textMargin;
  1828.     WESetDestRect(&dr, macWE);
  1829.  
  1830.     GetAperture(&fr);
  1831.     FrameToQDR(&fr, &r);
  1832.     QDToLongRect(&r, &fr);
  1833.     WESetViewRect(&fr, macWE);
  1834. }
  1835.  
  1836. /********************************************************\
  1837.  ResizeFrame - resize the frame when the size of the
  1838.      pane changes
  1839. \********************************************************/
  1840.  
  1841. void CWASTEText::ResizeFrame(Rect *delta)
  1842. {
  1843.     Boolean    saveAllocState;
  1844.     OSErr        err;
  1845.  
  1846.     CAbstractText::ResizeFrame(delta);
  1847.  
  1848.     CalcWERects();    
  1849.  
  1850.     if (lineWidth < 0 && (delta->left !=0 || delta->right != 0))
  1851.     {
  1852.         saveAllocState = SetAllocation(kAllocCanFail);
  1853.         err = WECalText(macWE);
  1854.         SetAllocation(saveAllocState);
  1855.         CalcWERects();    
  1856.     }
  1857.  
  1858.     AdjustBounds();
  1859. }
  1860.  
  1861. /********************************************************\
  1862.  AdjustBounds - change the size of the CWASTEText to
  1863.      match that in the WASTE record
  1864. \********************************************************/
  1865.  
  1866. void CWASTEText::AdjustBounds()
  1867. {
  1868.     LongRect    oldBounds;
  1869.     long          newHeight;
  1870.     long        hFix = 0, vFix = 0;
  1871.  
  1872.     oldBounds = bounds;
  1873.     newHeight = GetHeight(0, MAXINT);
  1874.     
  1875.     bounds.left = bounds.top = 0;
  1876.     bounds.bottom = newHeight;
  1877.  
  1878.     if (lineWidth > 0) 
  1879.     {
  1880.         bounds.right = lineWidth;
  1881.     }
  1882.     else
  1883.     {
  1884.         bounds.right = frame.right - frame.left;
  1885.     }
  1886.         
  1887.     bounds.right = (bounds.right - 1) / hScale + 1;
  1888.  
  1889.     if (itsScrollPane != NULL)
  1890.     {
  1891.         itsScrollPane->AdjustScrollMax();
  1892.         itsScrollPane->Calibrate();
  1893.     }
  1894. }
  1895.  
  1896. /********************************************************\
  1897.  FindLine - return the line on which the character
  1898.      at charPos is on.
  1899. \********************************************************/
  1900.  
  1901. long CWASTEText::FindLine(long charPos)
  1902. {
  1903.     long    lineNo;
  1904.     
  1905. #if WASTE_VERSION >= 0x01100000
  1906.     lineNo = WEOffsetToLine(charPos, macWE);
  1907. #else
  1908.     lineNo = _WEOffsetToLine(charPos, macWE);
  1909. #endif
  1910.     if (charPos == WEGetTextLength(macWE) && 
  1911.         ((char*)(*GetTextHandle()))[GetLength()-1] == 13)
  1912.         lineNo++;
  1913.  
  1914.     return (lineNo);
  1915.     
  1916. }
  1917.  
  1918. /********************************************************\
  1919.  GetLength - return the length of the text
  1920. \********************************************************/
  1921.  
  1922. long CWASTEText::GetLength()
  1923. {
  1924.     return WEGetTextLength(macWE);
  1925. }
  1926.  
  1927. /********************************************************\
  1928.  SetFontNumber - set the font of the selection
  1929. \********************************************************/
  1930.  
  1931. void CWASTEText::SetFontNumber(short aFontNumber)
  1932. {
  1933.     TextStyle    style;
  1934.     
  1935.     style.tsFont = aFontNumber;
  1936.     SetStyle( doFont, &style, TRUE);
  1937. //    itsSupervisor->Notify(NULL); // can't undo
  1938.  
  1939. }
  1940.  
  1941. /********************************************************\
  1942.  SetFontNumberAll - set the font of all of the text
  1943. \********************************************************/
  1944.  
  1945. void CWASTEText::SetFontNumberAll(short aFontNumber)
  1946. {
  1947.     TempSelectAll();
  1948.     SetFontNumber(aFontNumber);
  1949.     RestoreSelection();
  1950. //    itsSupervisor->Notify(NULL); // can't undo
  1951. }
  1952.  
  1953. /********************************************************\
  1954.  SetFontNameAll - set the font of the selection
  1955. \********************************************************/
  1956.  
  1957. void CWASTEText::SetFontNameAll(Str255 aFontName)
  1958. {
  1959.     TempSelectAll();
  1960.     SetFontName(aFontName);
  1961.     RestoreSelection();
  1962. //    itsSupervisor->Notify(NULL); // can't undo
  1963. }
  1964.  
  1965. /********************************************************\
  1966.  SetFontStyle - set the font style of the selection
  1967. \********************************************************/
  1968.  
  1969. void CWASTEText::SetFontStyle(short aStyle)
  1970. {
  1971.     TextStyle    style;
  1972.     short        mode = doFace;
  1973.     
  1974.     style.tsFace = aStyle;
  1975.     if (aStyle != NOTHING)
  1976.         mode += doToggle;
  1977.     SetStyle( mode, &style, TRUE);
  1978. //    itsSupervisor->Notify(NULL); // can't undo
  1979. }
  1980.  
  1981. /********************************************************\
  1982.  SetFontStyleAll - set the font style of all of the text
  1983. \********************************************************/
  1984.  
  1985. void CWASTEText::SetFontStyleAll(short aStyle)
  1986. {
  1987.     TempSelectAll();
  1988.     SetFontStyle(aStyle);
  1989.     RestoreSelection();
  1990. //    itsSupervisor->Notify(NULL); // can't undo
  1991. }
  1992.  
  1993. /********************************************************\
  1994.  SetFontSize - set the font size of the selection
  1995. \********************************************************/
  1996.  
  1997. void CWASTEText::SetFontSize(short aSize)
  1998. {
  1999.     TextStyle    style;
  2000.     
  2001.     style.tsSize = aSize;
  2002.     SetStyle( doSize, &style, TRUE);
  2003. //    itsSupervisor->Notify(NULL); // can't undo
  2004. }
  2005.  
  2006. /********************************************************\
  2007.  SetFontSizeAll - set the font size of all of the text
  2008. \********************************************************/
  2009.  
  2010. void CWASTEText::SetFontSizeAll(short aSize)
  2011. {
  2012.     TempSelectAll();
  2013.     SetFontSize(aSize);
  2014.     RestoreSelection();
  2015. //    itsSupervisor->Notify(NULL); // can't undo
  2016. }
  2017.  
  2018. /********************************************************\
  2019.  SetFontColor - set the color of the selection
  2020. \********************************************************/
  2021.  
  2022. void CWASTEText::SetFontColor(RGBColor *aFontColor)
  2023. {
  2024.     TextStyle    style;
  2025.     
  2026.     style.tsColor = (*aFontColor);
  2027.     SetStyle(doColor, &style, TRUE);
  2028. //    itsSupervisor->Notify(NULL); // can't undo
  2029.  
  2030. }
  2031.  
  2032. /********************************************************\
  2033.  SetFontColorAll - set the font of all of the text
  2034. \********************************************************/
  2035.  
  2036. void CWASTEText::SetFontColorAll(RGBColor *aFontColor)
  2037. {
  2038.     TempSelectAll();
  2039.     SetFontColor(aFontColor);
  2040.     RestoreSelection();
  2041. //    itsSupervisor->Notify(NULL); // can't undo
  2042. }
  2043.  
  2044. /********************************************************\
  2045.  SetTextMode - currently not implemented
  2046. \********************************************************/
  2047.  
  2048. void CWASTEText::SetTextMode(short aMode)
  2049. {
  2050.     return;
  2051. }
  2052.  
  2053. /********************************************************\
  2054.  SetAlignment - set the alignment
  2055. \********************************************************/
  2056.  
  2057. void CWASTEText::SetAlignment(short anAlignment)
  2058. {
  2059.     WESetAlignment(anAlignment, macWE);
  2060.     
  2061.     Refresh();
  2062. }
  2063.  
  2064. /********************************************************\
  2065.  SetAlignCmd - set the alignment
  2066. \********************************************************/
  2067.  
  2068. void CWASTEText::SetAlignCmd(long anAlignCmd)
  2069. {
  2070.     short teAlign;
  2071.     
  2072.     alignCmd = anAlignCmd;
  2073.     switch( alignCmd)
  2074.     {
  2075.         case cmdAlignLeft:
  2076.             teAlign = weFlushLeft;
  2077.             break;
  2078.         case cmdAlignCenter:
  2079.             teAlign = weCenter;
  2080.             break;
  2081.         case cmdAlignRight:
  2082.             teAlign = weFlushRight;
  2083.             break;
  2084.         case cmdJustify:        //mf
  2085.             teAlign = weJustify;
  2086.             break;
  2087.         default:
  2088.             teAlign = weFlushDefault;
  2089.     }
  2090.     SetAlignment(teAlign);
  2091. }
  2092.  
  2093. /********************************************************\
  2094.  SetSpacingCmd - not really supported
  2095. \********************************************************/
  2096.  
  2097. void CWASTEText::SetSpacingCmd(long aSpacingCmd)
  2098. {
  2099.     spacingCmd = cmdSingleSpace;
  2100.     
  2101.     // only single-spaced text is supported.
  2102.     
  2103.     SetWholeLines(wholeLines);
  2104.      CalcAperture();
  2105.     AdjustBounds();
  2106.  
  2107. }
  2108.  
  2109. /********************************************************\
  2110.  SetTheStyleScrap - set the style of a given range
  2111.      caller disposes handle
  2112. \********************************************************/
  2113.  
  2114. void CWASTEText::SetTheStyleScrap(long rangeStart, long rangeEnd,
  2115.             StScrpHandle styleScrap, Boolean fRedraw)
  2116. {
  2117.     Boolean        saveAllocState;
  2118.     OSErr        err;
  2119.     long        selStart, selEnd;
  2120.     
  2121.     Prepare();
  2122.  
  2123.     if (!fRedraw) InhibitRedraw(true);
  2124.  
  2125.     if (!ReallyVisible())
  2126.         SetOrigin(-10000, -10000);
  2127.     saveAllocState = SetAllocation(kAllocCanFail);
  2128.     WEGetSelection(&selStart, &selEnd, macWE);
  2129.     SetSelection(rangeStart, rangeEnd, FALSE);
  2130.     err = WEUseStyleScrap(styleScrap, macWE);
  2131.     if (err == noErr)
  2132.     {
  2133.         SetSelection(selStart, selEnd, false);
  2134.         err = WECalText( macWE);
  2135.     }
  2136.  
  2137.     SetAllocation(saveAllocState);
  2138.     AdjustBounds();
  2139. //    itsSupervisor->Notify(NULL); // can't undo
  2140.  
  2141.     if (!fRedraw) InhibitRedraw(false);
  2142.  
  2143.     FailOSErr(err);
  2144. }
  2145.  
  2146. /********************************************************\
  2147.  SetStyle - set the style of the current selection
  2148. \********************************************************/
  2149.  
  2150. void CWASTEText::SetStyle(short mode, TextStyle *newStyle, Boolean fRedraw)
  2151. {
  2152.     Boolean        saveAllocState;
  2153.     OSErr        err;
  2154. #if WASTE_VERSION >= 0x01100000
  2155.     Boolean        wasReadOnly;
  2156. #endif
  2157.  
  2158.     Prepare();
  2159.  
  2160.     if (!fRedraw) InhibitRedraw(true);
  2161.  
  2162.     if (!ReallyVisible())
  2163.         SetOrigin(-10000, -10000);
  2164.     saveAllocState = SetAllocation(kAllocCanFail);
  2165. #if WASTE_VERSION >= 0x01100000
  2166.     wasReadOnly = WEFeatureFlag(weFReadOnly, weBitClear, macWE);
  2167. #endif
  2168.     err = WESetStyle( mode, newStyle, macWE);
  2169. #if WASTE_VERSION >= 0x01100000
  2170.     WEFeatureFlag(weFReadOnly, wasReadOnly, macWE);
  2171. #endif
  2172.     SetAllocation(saveAllocState);
  2173.  
  2174.     if (err == noErr)
  2175.     {
  2176.         SetSpacingCmd(spacingCmd);
  2177.         SetWholeLines(wholeLines);
  2178.     }
  2179.     
  2180.     AdjustBounds();
  2181.  
  2182. //    itsSupervisor->Notify(NULL); // can't undo
  2183.  
  2184.     if (!fRedraw) InhibitRedraw(false);
  2185.  
  2186.     FailOSErr(err);
  2187. }
  2188.  
  2189. /********************************************************\
  2190.  GetHeight - get the height of the indicated lines
  2191. \********************************************************/
  2192.  
  2193. long CWASTEText::GetHeight(long startLine, long endLine)
  2194. {
  2195.     long height;
  2196.     long nLines;
  2197.     
  2198.     nLines = GetNumLines();
  2199.     height = WEGetHeight( startLine - 1, endLine, macWE);
  2200.     if (endLine >= nLines && ((char*)(*GetTextHandle()))[GetLength()-1] == 13)
  2201.         height += WEGetHeight(nLines - 2, nLines - 1, macWE);
  2202.  
  2203.     return height;
  2204. }
  2205.  
  2206. /********************************************************\
  2207.  GetCharOffset - return offset of character at point in
  2208.      frame coords
  2209. \********************************************************/
  2210.  
  2211. long CWASTEText::GetCharOffset(LongPt *aPt)
  2212. {
  2213.     Point    qdPt;
  2214.     LongPt lPt;
  2215.     char edge;
  2216.     
  2217.     Prepare();
  2218.     FrameToQD( aPt, &qdPt);
  2219.     lPt.v = qdPt.v;
  2220.     lPt.h = qdPt.h;
  2221.     
  2222.     return WEGetOffset( &lPt, &edge, macWE);
  2223.     
  2224. }
  2225.  
  2226. /********************************************************\
  2227.  GetCharPoint - return the position of the character in
  2228.      Frame coordinates
  2229. \********************************************************/
  2230.  
  2231. void CWASTEText::GetCharPoint( long offset, LongPt *aPt)
  2232. {
  2233.     Point    qdPt;
  2234.     LongPt lPt;
  2235.     short lineHeight;
  2236.     
  2237. #if TCL_VERSION >= 0x02000000
  2238.     TCL_ASSERT( offset <= MAXLONG);
  2239. #else
  2240.     ASSERT( offset <= MAXLONG);
  2241. #endif
  2242.     
  2243.     Prepare();
  2244.     WEGetPoint(offset, &lPt, &lineHeight, macWE);
  2245.     qdPt.h = lPt.h;
  2246.     qdPt.v = lPt.v;
  2247.     QDToFrame( qdPt, aPt);
  2248. }
  2249.  
  2250. /********************************************************\
  2251.  GetTextStyle - return style of selection
  2252. \********************************************************/
  2253.  
  2254. void CWASTEText::GetTextStyle(short *whichAttributes, TextStyle *aStyle)
  2255. {
  2256.     WEContinuousStyle(whichAttributes, aStyle, macWE);
  2257. }
  2258.  
  2259. /********************************************************\
  2260.  GetTextStyles -- return a style scrap for all of the
  2261.      text.
  2262. \********************************************************/
  2263.  
  2264. StScrpHandle CWASTEText::GetTextStyles(void)
  2265. {
  2266.     OSErr            err;
  2267.     StScrpHandle    styles;
  2268.     Boolean            saveAllocState;
  2269.     
  2270.     styles = (StScrpHandle) NewHandleCanFail(0);
  2271.     saveAllocState = SetAllocation(kAllocCanFail);
  2272. #if WASTE_VERSION >= 0x01100000
  2273.     err = WECopyRange(0, WEGetTextLength(macWE), NULL, styles, NULL, macWE);
  2274. #else
  2275.     err = WECopyRange(0, WEGetTextLength(macWE), NULL, styles, macWE);
  2276. #endif
  2277.     SetAllocation(saveAllocState);
  2278.     if (err != noErr)
  2279.     {
  2280.         DisposeHandle((Handle) styles);
  2281.         FailOSErr(err);
  2282.     }
  2283.  
  2284.     return (styles);
  2285. }
  2286.  
  2287. /********************************************************\
  2288.  GetCharStyle - return style of one character
  2289. \********************************************************/
  2290.  
  2291. void CWASTEText::GetCharStyle(long charOffset, TextStyle *theStyle)
  2292. {
  2293.     WERunInfo runInfo;
  2294.  
  2295.     WEGetRunInfo(charOffset, &runInfo, macWE);
  2296.     *theStyle = runInfo.runStyle;
  2297. }
  2298.  
  2299. /********************************************************\
  2300. GetRunInfo - get run info in a WERunInfo structure
  2301. \********************************************************/
  2302.  
  2303. void CWASTEText::GetRunInfo(long offset, WERunInfo *runInfo)
  2304. {
  2305.     WEGetRunInfo(offset, runInfo, macWE);
  2306. }
  2307.  
  2308. /********************************************************\
  2309. GetChar -- return the character at an offset, handling
  2310.     2 byte characters correctly
  2311. \********************************************************/
  2312.  
  2313. short CWASTEText::GetChar(long offset)
  2314. {
  2315.     return WEGetChar(offset, macWE);
  2316. }
  2317.  
  2318. /********************************************************\
  2319.  GetSpacingCmd - not really supported
  2320. \********************************************************/
  2321.  
  2322. long CWASTEText::GetSpacingCmd(void)
  2323. {
  2324.     return spacingCmd;
  2325. }
  2326.  
  2327. /********************************************************\
  2328.  GetAlignCmd - not really supported
  2329. \********************************************************/
  2330.  
  2331. long CWASTEText::GetAlignCmd(void)
  2332. {
  2333.     return alignCmd;
  2334. }
  2335.  
  2336. /********************************************************\
  2337.  GetTheStyleScrap -- get a scrap handle of the styles
  2338.      of the selection
  2339. \********************************************************/
  2340.  
  2341. StScrpHandle CWASTEText::GetTheStyleScrap(void)
  2342. {
  2343.     Boolean        saveAllocState;
  2344.     OSErr        err;
  2345.     long        selStart, selEnd;
  2346.     StScrpHandle h;
  2347.  
  2348.     GetSelection(&selStart, &selEnd);
  2349.  
  2350.     h=(StScrpHandle)NewHandleCanFail(1);
  2351.     FailNIL(h);
  2352.     saveAllocState = SetAllocation(kAllocCanFail);
  2353. #if WASTE_VERSION >= 0x01100000
  2354.     err = WECopyRange(selStart, selEnd, (Handle)NULL, h, NULL, macWE);
  2355. #else
  2356.     err = WECopyRange(selStart, selEnd, (Handle)NULL, h, macWE);
  2357. #endif
  2358.     SetAllocation(saveAllocState);
  2359.     FailOSErr(err);
  2360.     
  2361.     return h;
  2362. }
  2363.  
  2364. #if WASTE_VERSION >= 0x01100000
  2365. /********************************************************\
  2366.  GetTheSoup - return the soup of the selection
  2367. \********************************************************/
  2368.  
  2369. WESoupHandle CWASTEText::GetTheSoup(void)
  2370. {
  2371.     Boolean        saveAllocState;
  2372.     OSErr        err;
  2373.     long        selStart, selEnd;
  2374.     WESoupHandle h;
  2375.  
  2376.     GetSelection(&selStart, &selEnd);
  2377.  
  2378.     h=(WESoupHandle)NewHandleCanFail(1);
  2379.     FailNIL(h);
  2380.     saveAllocState = SetAllocation(kAllocCanFail);
  2381.     err = WECopyRange(selStart, selEnd, (Handle)NULL, NULL, h, macWE);
  2382.     SetAllocation(saveAllocState);
  2383.     FailOSErr(err);
  2384.     
  2385.     return h;
  2386. }
  2387. #endif
  2388.  
  2389. /********************************************************\
  2390.  GetNumLines - return the number of lines
  2391. \********************************************************/
  2392.  
  2393. long CWASTEText::GetNumLines(void)
  2394. {
  2395.     long nLines = WECountLines(macWE);
  2396.  
  2397.     if (!GetLength()) return 0;
  2398.     // add 1 if last character is a newline
  2399.     if (((char*)(*GetTextHandle()))[GetLength()-1] == 13)
  2400.         nLines++;
  2401.     return nLines;
  2402. }
  2403.  
  2404. /********************************************************\
  2405.  GetSelection - get the position of the start and end
  2406.      of the selection
  2407. \********************************************************/
  2408.  
  2409. void CWASTEText::GetSelection(long *selStart, long *selEnd)
  2410. {
  2411.     WEGetSelection(selStart, selEnd, macWE);
  2412. }
  2413.  
  2414. /********************************************************\
  2415.  HideSelection - not implemented
  2416. \********************************************************/
  2417.  
  2418. void CWASTEText::HideSelection(Boolean hide, Boolean redraw)
  2419. {
  2420.     return;
  2421. }
  2422.  
  2423. /********************************************************\
  2424.  GetSteps - get the size of the scrolling steps
  2425.      Step sizes hard coded in
  2426. \********************************************************/
  2427.  
  2428. void CWASTEText::GetSteps(short *hStep, short *vStep)
  2429. {
  2430.     long        nLines,
  2431.                 height;
  2432.  
  2433.     nLines = GetNumLines();
  2434.     height = GetHeight(0, MAXINT);
  2435.     *hStep = 20;
  2436.     if (nLines)
  2437. #if TCL_VERSION >= 0x02000000
  2438.         *vStep = TCLMin(height / nLines, 30);
  2439. #else
  2440.         *vStep = Min(height / nLines, 30);
  2441. #endif
  2442.     else
  2443.         *vStep = height;
  2444. }
  2445.  
  2446. /********************************************************\
  2447.  AboutToPrint -- called right before printing
  2448. \********************************************************/
  2449.  
  2450. void CWASTEText::AboutToPrint(short *firstPage, short *lastPage)
  2451. {
  2452.     if (active)
  2453.     {
  2454.         Prepare();
  2455.         HidePen();
  2456.         WEDeactivate(macWE);
  2457.         ShowPen();
  2458.     }
  2459.  
  2460.     CAbstractText::AboutToPrint(firstPage, lastPage);
  2461. }
  2462.  
  2463.  
  2464. /********************************************************\
  2465.  Paginate -- figure out the number of pages
  2466. \********************************************************/
  2467.  
  2468. void CWASTEText::Paginate(CPrinter *aPrinter, short pageWidth, short pageHeight)
  2469. {
  2470.     Boolean        saveAllocState;
  2471.     OSErr        err;
  2472.     LongRect    dr;
  2473.  
  2474.     WEGetDestRect(&dr, macWE);
  2475.     dr.right = dr.left + pageWidth;
  2476.     WESetDestRect(&dr, macWE);
  2477.     saveAllocState = SetAllocation(kAllocCanFail);
  2478.     err = WECalText(macWE);
  2479.     SetAllocation(saveAllocState);
  2480.     FailOSErr(err);
  2481.     printPageWidth = pageWidth;
  2482.     
  2483.     CAbstractText::Paginate(aPrinter, pageWidth, pageHeight);
  2484. }
  2485.  
  2486. /********************************************************\
  2487.  DonePrinting - called when printing is finished
  2488. \********************************************************/
  2489.  
  2490. void CWASTEText::DonePrinting()
  2491. {
  2492.     Boolean        saveAllocState;
  2493.     OSErr        err;
  2494.  
  2495.     CAbstractText::DonePrinting();
  2496.  
  2497.     if (active)
  2498.     {
  2499.         Prepare();
  2500.         HidePen();
  2501.         WEActivate(macWE);
  2502.         ShowPen();
  2503.     }
  2504.  
  2505.     CalcWERects();
  2506.     if (lineWidth < 0)
  2507.     {
  2508.         saveAllocState = SetAllocation(kAllocCanFail);
  2509.         err = WECalText( macWE);
  2510.         SetAllocation(saveAllocState);
  2511.     }
  2512. }
  2513.  
  2514. /********************************************************\
  2515.  Dawdle - do idle stuff
  2516. \********************************************************/
  2517.  
  2518. void CWASTEText::Dawdle(long *maxSleep)
  2519. {
  2520.     if (visible)
  2521.     {
  2522.         Prepare();
  2523.         WEIdle(NULL, macWE);
  2524.         *maxSleep = GetCaretTime();
  2525.     }
  2526. }
  2527.  
  2528. #if TCL_VERSION >= 0x02000000
  2529.  
  2530. #if VA_COMPATIBLE_IO
  2531. /********************************************************\
  2532.  GetFrom -- VA/CEditText compatible version 
  2533.     Code by Michael Grinner
  2534.     Note: Style and soup not saved
  2535. \********************************************************/
  2536.  
  2537. void CWASTEText::GetFrom(CStream& stream)
  2538. {
  2539. #ifdef TCL_OBJECT_IO
  2540.     Handle        volatile h = NULL;        // Text if len > 255
  2541.     Str255        text;                    // Text if len < 256
  2542.     short        txFont, txFace, txMode, txSize, textLength;
  2543.     short        reserved;
  2544.     GrafPtr        port;
  2545.  
  2546.     stream >> spacingCmd >> alignCmd >> reserved;
  2547.     stream >> txFace >> txMode >> txSize >> textLength;
  2548.  
  2549.     stream.GetStr255(text);            // Get font id
  2550.     GetFNum (text, &txFont);
  2551.  
  2552.  
  2553. // Go out of our way to avoid
  2554. //   creating a useless handle,
  2555. //   since most strings are small
  2556. //   and MM is *slow*.
  2557.  
  2558.     try_
  2559.     {
  2560.         if (textLength < 256)
  2561.         {
  2562.             stream.Get(&text[1], textLength);
  2563.             text[0] = textLength;
  2564.         }
  2565.         else
  2566.         {
  2567.             h = NewHandleCanFail(textLength);
  2568.             FailNIL(h);
  2569.             HLock(h);
  2570.             stream.Get(*h, textLength);
  2571.             HUnlock(h);
  2572.         }
  2573.  
  2574.         CAbstractText::GetFrom(stream); // Let base class restore and in it
  2575.  
  2576.         GetPort(&port);
  2577.         SetPort(macPort);
  2578.         TextFont(txFont);
  2579.         TextFace(txFace);
  2580.         TextSize(txSize);
  2581.         TextMode(txMode);
  2582.         SetPort(port);
  2583.  
  2584.         IWASTETextX(true, true, true, true, true);
  2585.  
  2586.         if (textLength < 256)
  2587.         {
  2588.             SetTextString(text);
  2589.         }
  2590.         else
  2591.         {
  2592.             SetTextHandle(h);                       // Set text
  2593.         }
  2594.         SetAlignCmd(alignCmd);                  // Set justification
  2595.         SetSpacingCmd(spacingCmd);              // Set line spacing
  2596.     }
  2597.     catch_all_()
  2598.     {
  2599.         TCLForgetHandle(h);
  2600.         throw_same_();
  2601.     }
  2602.     end_try_
  2603.     TCLForgetHandle(h);
  2604. #endif
  2605. }
  2606.  
  2607. /******************************************************************************
  2608.  PutTo
  2609.     CEditText compatible PutTo
  2610.  ******************************************************************************/
  2611.  
  2612. void CWASTEText::PutTo(CStream& stream)
  2613. {
  2614. #ifdef TCL_OBJECT_IO
  2615.     Handle            h;                  // Text contents
  2616.     SignedByte        state;
  2617.     short            txFont, txFace, txMode, txSize, textLength;
  2618.     Str255            fontName;
  2619.     TextStyle        theStyle;
  2620.  
  2621.     stream << spacingCmd << alignCmd << (short) 0;
  2622.     GetCharStyle(0, &theStyle);
  2623.     txFace = theStyle.tsFace;
  2624.     txMode = srcCopy; // not really supported
  2625.     txSize = theStyle.tsSize;
  2626.     h = GetTextHandle();
  2627.     textLength = GetHandleSize(h);
  2628.     stream << txFace << txMode << txSize << textLength;
  2629.  
  2630.     txFont = theStyle.tsFont;            // Save font name
  2631.     GetFontName(txFont, fontName);
  2632.     stream.PutStr255(fontName);
  2633.  
  2634.     state = HGetState(h);
  2635.     if ((state & 0x80) == 0) {            // If not already locked
  2636.         MoveHHi(h);
  2637.         HLock(h);
  2638.     }
  2639.     try_
  2640.     {
  2641.         stream.Put(*h, textLength);
  2642.     }
  2643.     catch_all_()
  2644.     {
  2645.         HSetState(h, state);
  2646.         throw_same_();
  2647.     }
  2648.     end_try_
  2649.     HSetState(h, state);
  2650.  
  2651.     CAbstractText::PutTo(stream);        // Let base class save
  2652. #endif
  2653. }
  2654.  
  2655. #else // normal version of I/O
  2656.  
  2657. /********************************************************\
  2658.  GetFrom -  read a CWASTEText from a stream
  2659. \********************************************************/
  2660.  
  2661. void CWASTEText::GetFrom(CStream& stream)
  2662. {
  2663. #ifdef TCL_OBJECT_IO
  2664.         Boolean supportObjects = false;
  2665.         Boolean supportUndo, supportDragAndDrop, outlineHighliting, drawOffscreen;
  2666.  
  2667.         stream >> spacingCmd >> alignCmd;
  2668.  
  2669.         inherited::GetFrom(stream);
  2670.  
  2671. #if WASTE_OBJECTS
  2672.         stream >> supportObjects;
  2673. #endif
  2674.         stream >> supportUndo >> supportDragAndDrop >> outlineHighliting >> drawOffscreen;
  2675.         
  2676.         volatile Handle                 hText = NULL;
  2677.         volatile StScrpHandle   hStyles = NULL;
  2678.         volatile WESoupHandle   hSoup = NULL;
  2679.  
  2680.         try_
  2681.         {
  2682.                 IWASTETextX(supportObjects, supportUndo, supportDragAndDrop, outlineHighliting, drawOffscreen);
  2683.  
  2684.                 hText = stream.GetHandle();
  2685.                 hStyles = (StScrpHandle) stream.GetHandle();
  2686.                 hSoup = stream.GetHandle();
  2687.  
  2688.                 HLock(hText);
  2689.                 InsertWithStyleSoup(*hText, GetHandleSize(hText), hStyles, hSoup, FALSE);
  2690.                 HUnlock(hText);
  2691.  
  2692.                 TCLForgetHandle (hText);
  2693.                 TCLForgetHandle (hStyles);
  2694.                 TCLForgetHandle (hSoup);
  2695.         }
  2696.         catch_all_()
  2697.         {
  2698.                 TCLForgetHandle (hText);
  2699.                 TCLForgetHandle (hStyles);
  2700.                 TCLForgetHandle (hSoup);
  2701.  
  2702.                 throw_same_();
  2703.         }
  2704.         end_try_
  2705. #endif
  2706.  
  2707. }
  2708.  
  2709. /********************************************************\
  2710.  PutTo - write a CWASTEText to a stream
  2711. \********************************************************/
  2712.  
  2713. void CWASTEText::PutTo(CStream& stream)
  2714. {
  2715. #ifdef TCL_OBJECT_IO
  2716.     stream << spacingCmd << alignCmd;
  2717.  
  2718.     inherited::PutTo(stream);
  2719.  
  2720. #if WASTE_OBJECTS
  2721.     stream << objHandersInstalled;
  2722. #endif
  2723.     Boolean supportUndo = (WEFeatureFlag(weFUndo, weBitTest, macWE) == weBitSet);
  2724.     Boolean supportDragAndDrop = (WEFeatureFlag(weFDragAndDrop, weBitTest, macWE) == weBitSet);
  2725.     Boolean outlineHighliting = (WEFeatureFlag(weFOutlineHilite, weBitTest, macWE) == weBitSet);
  2726.     Boolean drawOffscreen = (WEFeatureFlag(weFDrawOffscreen, weBitTest, macWE) == weBitSet);
  2727.  
  2728.     stream << supportUndo << supportDragAndDrop << outlineHighliting << drawOffscreen;
  2729.     
  2730.     Handle            hText = NewHandle (0);
  2731.     StScrpHandle    hStyles = (StScrpHandle) NewHandle(0);
  2732.     WESoupHandle    hSoup = (WESoupHandle)NewHandle(0);
  2733.     long             end = WEGetTextLength(macWE);
  2734.  
  2735.     FailNIL(hSoup);
  2736.     FailNIL(hText);
  2737.     FailNIL(hStyles);
  2738.  
  2739.     try_
  2740.     {
  2741.         CopyRangeWithStyleSoup(0, end, hText, NULL, NULL);
  2742.         stream.PutHandle(hText);
  2743.         TCLForgetHandle(hText);
  2744.         
  2745.         CopyRangeWithStyleSoup(0, end, NULL, hStyles, NULL);
  2746.         stream.PutHandle((Handle) hStyles);
  2747.         TCLForgetHandle(hStyles);
  2748.         
  2749.         CopyRangeWithStyleSoup(0, end, NULL, NULL, hSoup);
  2750.         stream.PutHandle(hSoup);
  2751.         TCLForgetHandle(hSoup);
  2752.     }
  2753.     catch_all_()
  2754.     {
  2755.         TCLForgetHandle(hText);
  2756.         TCLForgetHandle(hStyles);
  2757.         TCLForgetHandle(hSoup);
  2758.         
  2759.         throw_same_();
  2760.     }
  2761.     end_try_
  2762.  
  2763. #endif
  2764. }
  2765.  
  2766. #endif // VA_COMPATIBLE_IO
  2767.  
  2768. #endif // TCL 2.0
  2769.  
  2770. /********************************************************\
  2771.  WEClickLoop - click loop routine
  2772. \********************************************************/
  2773.  
  2774. pascal Boolean CWASTEText::WEClickLoop(WEHandle hWE)
  2775. {
  2776.     Point        mouseLoc;
  2777.     LongPt        longMouse;
  2778.     CWASTEText *itsOwner = NULL;
  2779.  
  2780.     WEGetInfo(weRefCon, (Ptr)&itsOwner, hWE);
  2781. #if TCL_VERSION >= 0x02000000
  2782.     TCL_ASSERT_OBJECT_PTR(itsOwner);
  2783.     TCL_ASSERT(member(itsOwner, CWASTEText));
  2784. #else
  2785.     ASSERT(member(itsOwner, CWASTEText));
  2786. #endif
  2787.     
  2788.     if (itsOwner != NULL)
  2789.     {
  2790.         GetMouse(&mouseLoc);
  2791.         itsOwner->QDToFrame(mouseLoc, &longMouse);
  2792.         itsOwner->AutoScroll( &longMouse);
  2793.     }
  2794.     return(TRUE);
  2795. }
  2796.  
  2797. // static variables for the following routines
  2798. static Boolean wasActive, outlineHilite;
  2799. static long selStart, selEnd;
  2800.  
  2801. /********************************************************\
  2802.  TempSelectAll -- temporarily selects all of the text
  2803.      while deactivating the text and turning off 
  2804.      outline hilighting so that the selection won't show
  2805.      up.  It can be restored by Restore selection.  This
  2806.      is useful when you want to apply something to all
  2807.      of the text without seeing all of the text flash.
  2808.      
  2809.      Note: Calls to TempSelectAll/RestoreSelection cannot
  2810.      be nested
  2811. \********************************************************/
  2812.  
  2813. void CWASTEText::TempSelectAll(void)
  2814. {
  2815.     // turn off outline highlighting temporarily
  2816.     outlineHilite = SetOutlineHighliting(false);
  2817.  
  2818.     // deactivate text
  2819.     wasActive = active;
  2820.     if (wasActive) Deactivate();
  2821.     
  2822.     // select all
  2823.     GetSelection(&selStart, &selEnd);
  2824.     SetSelection(0, 0x7FFFFFF, false);
  2825. }
  2826.     
  2827. /********************************************************\
  2828.  RestoreSelection - restores things to the way they
  2829.      were before a call to TempSelectAll()
  2830. \********************************************************/
  2831.  
  2832. void CWASTEText::RestoreSelection(void)
  2833. {
  2834.     if (wasActive) Activate();
  2835.     SetOutlineHighliting(outlineHilite);
  2836.     SetSelection(selStart, selEnd, false);
  2837. }
  2838.  
  2839. /******************************************************************************
  2840.  Specify
  2841.  
  2842.     In WASTE 1.0, if editable is changed and it's active, enable/disable TSMInput
  2843.  ******************************************************************************/
  2844.  
  2845. void CWASTEText::Specify(Boolean fEditable, Boolean fSelectable, Boolean fStylable)
  2846. {
  2847.     Boolean wasEditable = editable;
  2848.     Boolean oldWantsClicks = wantsClicks;
  2849. #if WASTE_VERSION == 0x01000000
  2850.     TSMDocumentID aTSMDocument;
  2851. #endif
  2852.     
  2853.     inherited::Specify(fEditable, fSelectable, fStylable);
  2854.     // don't let Specify change wantsClicks
  2855.     wantsClicks = oldWantsClicks;
  2856.  
  2857. #if WASTE_VERSION >= 0x01100000
  2858.     // use weFReadOnly flag
  2859.     if (macWE)
  2860.     {
  2861.         if (!fEditable) WEFeatureFlag(weFReadOnly, weBitSet, macWE);
  2862.         else WEFeatureFlag(weFReadOnly, weBitClear, macWE);
  2863.     }
  2864. #else
  2865.     if (active && wasEditable && !editable)
  2866.     {
  2867.         // if not editable, deactivate TSM so inline input won't work
  2868.         if (gUsingTSM)
  2869.         {
  2870.             if (WEGetInfo(weTSMDocumentID, (Ptr)&aTSMDocument, macWE) == noErr)
  2871.             {
  2872.                 if (aTSMDocument != NULL)
  2873.                     DeactivateTSMDocument(aTSMDocument);
  2874.             }
  2875.         }
  2876.     }
  2877.  
  2878.     if (active && !wasEditable && editable)
  2879.     {
  2880.         // if editable, activate TSM so inline input will work
  2881.         if (gUsingTSM)
  2882.         {
  2883.             if (WEGetInfo(weTSMDocumentID, (Ptr)&aTSMDocument, macWE) == noErr)
  2884.             {
  2885.                 if (aTSMDocument != NULL)
  2886.                     ActivateTSMDocument(aTSMDocument);
  2887.             }
  2888.         }
  2889.     }
  2890. #endif
  2891. }
  2892.  
  2893. /******************************************************************************
  2894.  AdjustCursor
  2895.  
  2896.     Use WEAdjustCursor to set the cursor
  2897.  ******************************************************************************/
  2898.  
  2899. void CWASTEText::AdjustCursor(Point where, RgnHandle mouseRgn)
  2900. {
  2901.     Point        wTopLeft;
  2902.  
  2903.     if (wantsClicks)
  2904.     {
  2905.         Prepare();
  2906.     
  2907.         // convert back to global coords
  2908.         wTopLeft = topLeft((**((WindowPeek)GetWindow()->macPort)->contRgn).rgnBBox);
  2909.         where.h += wTopLeft.h;
  2910.         where.v += wTopLeft.v;
  2911.  
  2912.         WEAdjustCursor(where, mouseRgn, macWE);
  2913.     }
  2914. }
  2915.  
  2916. /******************************************************************************
  2917.  InhibitRecal
  2918.     turn on or off inhibition of recalibration
  2919.  ******************************************************************************/
  2920.  
  2921. void CWASTEText::InhibitRecal(Boolean inhibit)
  2922. {
  2923.     WEFeatureFlag(weFInhibitRecal, inhibit ? weBitSet : weBitClear, macWE);
  2924. }
  2925.  
  2926. /******************************************************************************\
  2927.  InhibitRedraw
  2928.     turn on or off inhibition of redrawing
  2929. \******************************************************************************/
  2930.  
  2931. void CWASTEText::InhibitRedraw(Boolean inhibit)
  2932. {
  2933.     // ability to inhibit redraw was introduced in WASTE 1.2a3
  2934.  
  2935. #if WASTE_VERSION >= 0x01204003
  2936.     WEFeatureFlag(weFInhibitRedraw, inhibit ? weBitSet : weBitClear, macWE);
  2937. #endif
  2938. }
  2939.  
  2940. /******************************************************************************\
  2941.   SetLineWidth -- sets the line width
  2942.       contributed by Jan Petersen
  2943. \******************************************************************************/
  2944.  
  2945. void CWASTEText::SetLineWidth(short aLineWidth)
  2946. {
  2947.     lineWidth = aLineWidth;
  2948.     CalcWERects();
  2949. }
  2950.  
  2951. #if WASTE_OBJECTS
  2952. /******************************************************************************
  2953.  InsertPicture
  2954.     Insert a picture
  2955.  ******************************************************************************/
  2956.  
  2957. void CWASTEText::InsertPicture(PicHandle pHandle, Boolean fRedraw)
  2958. {
  2959.     Point zeroPoint = {0, 0};
  2960.     Boolean        saveAllocState;
  2961.     OSErr        err;
  2962.     Boolean        wasReadOnly;
  2963.  
  2964.     Prepare();
  2965.  
  2966.     if (!fRedraw) InhibitRedraw(true);
  2967.  
  2968.     if (!ReallyVisible())
  2969.         SetOrigin(-10000, -10000);
  2970.     CheckInsertion((*pHandle)->picSize, 0, TRUE);
  2971.     saveAllocState = SetAllocation(kAllocCanFail);
  2972.     wasReadOnly = WEFeatureFlag(weFReadOnly, weBitClear, macWE);
  2973.     err = WEInsertObject(kTypePicture, (Handle)pHandle, zeroPoint, macWE);
  2974.     WEFeatureFlag(weFReadOnly, wasReadOnly, macWE);
  2975.     SetAllocation(saveAllocState);
  2976.  
  2977.     AdjustBounds();
  2978.  
  2979.     if (!fRedraw) InhibitRedraw(false);
  2980.  
  2981.     FailOSErr(err);
  2982. }
  2983.  
  2984. /******************************************************************************
  2985.  InsertSound
  2986.     Insert a sound
  2987.  ******************************************************************************/
  2988.  
  2989. void CWASTEText::InsertSound(Handle sHandle, Boolean fRedraw)
  2990. {
  2991.     Point zeroPoint = {0, 0};
  2992.     Boolean        saveAllocState;
  2993.     OSErr        err;
  2994.     Boolean        wasReadOnly;
  2995.  
  2996.     Prepare();
  2997.  
  2998.     if (!fRedraw) InhibitRedraw(true);
  2999.  
  3000.     if (!ReallyVisible())
  3001.         SetOrigin(-10000, -10000);
  3002.     CheckInsertion(GetHandleSize(sHandle), 0, TRUE);
  3003.     saveAllocState = SetAllocation(kAllocCanFail);
  3004.     wasReadOnly = WEFeatureFlag(weFReadOnly, weBitClear, macWE);
  3005.     err = WEInsertObject(kTypeSound, sHandle, zeroPoint, macWE);
  3006.     WEFeatureFlag(weFReadOnly, wasReadOnly, macWE);
  3007.     SetAllocation(saveAllocState);
  3008.  
  3009.     AdjustBounds();
  3010.  
  3011.     if (!fRedraw) InhibitRedraw(false);
  3012.  
  3013.     FailOSErr(err);
  3014. }
  3015.  
  3016. #if WASTE_OBJECT_ARCHIVE
  3017. /******************************************************************************
  3018.  InsertFSSpec
  3019.     Insert a file
  3020.  ******************************************************************************/
  3021.  
  3022. void CWASTEText::InsertFSSpec(FSSpec *theFSSpec, Boolean fRedraw)
  3023. {
  3024.     Point zeroPoint = {0, 0};
  3025.     Boolean        saveAllocState;
  3026.     OSErr        err;
  3027.     Boolean        wasReadOnly;
  3028.     Handle        fsHandle;
  3029.  
  3030.     Prepare();
  3031.  
  3032.     if (!fRedraw) InhibitRedraw(true);
  3033.  
  3034.     if (!ReallyVisible())
  3035.         SetOrigin(-10000, -10000);
  3036.     fsHandle = NewHandle(sizeof(FSSpec));
  3037.     FailNIL(fsHandle);
  3038.     *((FSSpec *)*fsHandle) = *theFSSpec;
  3039.     saveAllocState = SetAllocation(kAllocCanFail);
  3040.     wasReadOnly = WEFeatureFlag(weFReadOnly, weBitClear, macWE);
  3041.     err = WEInsertObject(flavorTypeHFS, fsHandle, zeroPoint, macWE);
  3042.     WEFeatureFlag(weFReadOnly, wasReadOnly, macWE);
  3043.     SetAllocation(saveAllocState);
  3044.  
  3045.     AdjustBounds();
  3046.  
  3047.     if (!fRedraw) InhibitRedraw(false);
  3048.  
  3049.     FailOSErr(err);
  3050. }
  3051. #endif // WASTE_OBJECTS
  3052.  
  3053. #endif // WASTE 1.1
  3054.  
  3055. #if WASTE_VERSION >= 0x01100000
  3056.  
  3057. /******************************************************************************
  3058.  DoDrag
  3059.     
  3060.     See if a drag is started, and if so, handle click-through properly
  3061.  ******************************************************************************/
  3062.  
  3063. Boolean CWASTEText::DoDrag(EventRecord *theEvent, Point where)
  3064. {
  3065.     long selStart, selEnd;
  3066.     RgnHandle selRgn;
  3067.  
  3068.     if (!gHasDragAndDrop) return false;
  3069.     
  3070.     Prepare();
  3071.  
  3072.     WEGetSelection(&selStart, &selEnd, macWE);
  3073.     selRgn = WEGetHiliteRgn(selStart, selEnd, macWE);
  3074.     if (PtInRgn(where, selRgn) && WaitMouseMoved(theEvent->where))
  3075.     {
  3076.         // doing a drag -- just call DoClick now...
  3077.         DoClick(where, theEvent->modifiers, theEvent->when);
  3078.         return true;
  3079.     }
  3080.     return false;
  3081. }
  3082.  
  3083. /******************************************************************************
  3084.  GetUndoInfo - get information about what's in WASTE's undo stack
  3085.  ******************************************************************************/
  3086.  
  3087. WEActionKind CWASTEText::GetUndoInfo(Boolean *redoFlag)
  3088. {
  3089.     return WEGetUndoInfo(redoFlag, macWE);
  3090. }
  3091.  
  3092. /******************************************************************************
  3093.  DoUndo - perform undo operation
  3094.  ******************************************************************************/
  3095.  
  3096. void CWASTEText::DoUndo(void)
  3097. {
  3098.     FailOSErr(WEUndo(macWE));
  3099. }
  3100.  
  3101. #endif // WASTE 1.1
  3102.